0 Replies - 8317 Views - Last Post: 19 April 2012 - 11:16 PM

#1 Inevdt  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 29-February 12

Reading from text file into an Array

Posted 19 April 2012 - 11:16 PM

I'm trying to have my code read a from a text file into an array called Interest() but every time I try and run the program I get a filenotfound exception. I need the code to be portable so I'm using a relative path put it just doesn't seem to work. Any idea's?

Here is the complete code:

Imports System.IO




Partial Class _Default
    Inherits System.Web.UI.Page
    'Declaring Variables
    Dim iPrinciple As Double
    Dim Term() As Double = {7, 15, 30}
    Dim FinPayment As Double  'Is declared as MonthlyPayment in the code below to display in currency format
    Dim iRate As Double
    Dim iTerm As Double
    Dim TempInt As String = "InterestRates.txt"
    Dim Interest() As String = File.ReadAllLines(TempInt)


    Private Function MonthlyPayment() As String

        'Converts user input into double value for use in calculation
        iPrinciple = Val(Principle.Text)
        iRate = Val(InterestBox.Text)
        iTerm = Val(TermBox.Text)
        'This will select the correct term and rate amount based on what was chosen by the user
        If TRComboBox.SelectedItem.Text = "7 years at 5.35%" Then
            TermBox.Text = Term(0)
            InterestBox.Text = Interest(0)
        ElseIf TRComboBox.SelectedItem.Text = "15 years at 5.5%" Then
            TermBox.Text = Term(1)
            InterestBox.Text = Interest(1)
        ElseIf TRComboBox.SelectedItem.Text = "30 years at 5.75%" Then
            TermBox.Text = Term(2)
            InterestBox.Text = Interest(2)
        ElseIf TRComboBox.SelectedItem.Text = "Manual Entry" Then
        End If

        'IntFormat converts the Interest Rate into the monthly interest in decimal form
        'TermFormat converts the Term length of years into months
        'Power and Final are created to be used in the monthly payment
        Dim IntFormat As Double = iRate / (12 * 100)
        Dim TermFormat As Double = (iTerm * 12)
        Dim Power As Double = (1 + IntFormat)
        Dim Final As Double = Math.Pow(Power, -TermFormat)
        Final = (1 - Final) 'Calculates the final value
        Final = (1 / Final) 'Inverts the final value so that it is mulitplied correctly within the following equation

        'The following two lines calculate the monthly payment
        MonthlyPayment = iPrinciple * (IntFormat / 1 - (Math.Pow(Power, -TermFormat)))
        MonthlyPayment = (Final * iPrinciple * IntFormat)
        Return MonthlyPayment()

    End Function

    Protected Sub Calculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Calculate.Click
        'Error handling for invalid loan amount input
        If IsNumeric(Principle.Text) = False Then
            MsgBox("Invalid user input for principle. Please enter a numerical value excluding the $ sign.", MsgBoxStyle.Critical, "Loan Error")
            Principle.Text = ""

        End If

        If IsNumeric(InterestBox.Text) = False Then
            MsgBox("Invalid user input for interest. Please entern a numerical value excluding the % sign.")
            InterestBox.Text = ""
        End If

        If IsNumeric(TermBox.Text) = False Then
            MsgBox("Invalid user input for term length. Please enter a numberical value in year.")
            TermBox.Text = ""
        End If
        FinPayment = MonthlyPayment()


        Results.Text = FinPayment.ToString("C")
        Table(MonthlyPayment, iTerm, iRate, iPrinciple)
    End Sub

    Private Sub Table(ByVal MonthlyPayment As Double, ByVal iTerm As Double, _
                                           ByVal iRate As Double, ByVal iPrinciple As Double)

        ' Initialize variables
        Dim Year As Double = 0          '  temporary year value
        Dim Month As Double             '  temporary month value
        Dim Princ As Double              '  temporary loan value
        Dim PrevBal As Double               '  temporary previous value
        Dim Int As Double               '  temporary interest value
        Dim MInt As Double          '  temporary monthly interest value
        Dim MPrinc As Double    '  temporary monthly principal value
        Dim Output As String            '  output string
        Dim PMonth, PYear As Double ' For printout

        ' Set the interest rate
        Int = (iRate / (12 * 100))

        ' Set the loan amount
        Princ = iPrinciple
        PrevBal = Princ

        ' Clear the previous data
        TResults.Text = vbNullString


        TResults.Text = TResults.Text + (ControlChars.NewLine)

        ' Loop to print the amortization table for each month of each year of the mortgage
        Do While Year < iTerm


            ' To print the correct year
            PYear = (Year + 1)

            Month = 0   ' Reset the month
            Do While Month < 12


                ' To print the correct month
                PMonth = (Month + 1)

                ' Calculate the monthly interest
                MInt = (Princ * Int)
                ' Calculate the monthly principal
                MPrinc = (MonthlyPayment - MInt)

                ' Print out the principal and interest payment for this month

                ' Subtract the principal paid from the loan amount
                Princ = (Princ - MPrinc)

                If PrevBal > 0 Then
                    Output = " Month " & PMonth.ToString & " Payment " & PYear.ToString & ":     Principal = " _
                            & MPrinc.ToString("C") & ",     Int = " & MInt.ToString("C") & "     Balance = " _
                            & Princ.ToString("C") & ControlChars.NewLine

                    ' Print to the Amortization Table text box
                    TResults.Text = TResults.Text + (Output)

                    ' Save previous balance
                    PrevBal = Princ

                    ' Increase Month
                    Month += 1

                Else
                    ' Loan is finished, loop breaks
                    Month = 12
                    Year = iTerm
                End If

            Loop ' End of month loop

            ' Seperates the years
            TResults.Text = TResults.Text + (ControlChars.NewLine)



            Year += 1 ' Increase year
        Loop ' End of year loop

    End Sub


    Protected Sub Clear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Clear.Click
        Principle.Text = "200000"
        Results.Text = ""
        TResults.Text = ""
        InterestBox.Text = "5.75"
        TermBox.Text = "30"

    End Sub

    Protected Sub Kill_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Kill.Click

    End Sub
End Class



The exception is occurring on "Dim Interest() As String = File.ReadAllLines(TempInt)".

Dim TempInt As String = "InterestRates.txt"
    Dim Interest() As String = File.ReadAllLines(TempInt)


I've added the complete file of the program if that needs to be seen.

Attached File(s)

  • Attached File  15.zip (5.78K)
    Number of downloads: 44


Is This A Good Question/Topic? 0
  • +

Page 1 of 1