Results 1 to 11 of 11

Thread: Problems with getting contents from Readline() into Arrays

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    5

    Problems with getting contents from Readline() into Arrays

    For the most part, my program is working fine, but this procedure is given me issues when used

    http://pastie.org/3950400 .. I've messed around quite a bit with it and I can't seem to get it to work.. it either freezes the program or spits out an error like:

    >{"Index was outside the bounds of the array."}

    > System.IndexOutOfRangeException

    > Index was outside the bounds of the array.


    If I change line 38 to something like (40) from (-1) the program will just hang and eventually an error will pop up because it hasn't responded.

    Thanks in advance.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,378

    Re: Problems with getting contents from Readline() into Arrays

    Take a look at this explination on arrays. The next section in the tutorial explains the outside the bounds error you're getting.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    5

    Re: Problems with getting contents from Readline() into Arrays

    Thanks for the post, but I'm just unable to see where abouts I'm getting that code.. everything looks good to me. the -1 should be working I would have thought.. if I try any large value (the text tile only has up to 30 lines) it just hangs and freezes the program.

    Thanks.

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,378

    Re: Problems with getting contents from Readline() into Arrays

    I'm not going to open your project with the link provided. I would post the code wraped in 'code' tags. Because right now I'm only speculating what's up.

    Edit - Post the code where you use the array, not the whole project :P
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    5

    Re: Problems with getting contents from Readline() into Arrays

    vb Code:
    1. 'Variable used to locate the requested title by the user.
    2.         Dim strRequestedBook As String = InputBox("Please enter the title of the requested book", "Information Needed")
    3.  
    4.         'This will create an array which will be used to store each line from the textfile into. This will be used to process and find the required book
    5.         Dim strResultArray(99) As String
    6.         Dim intResultArrayIndex As Integer = 0
    7.  
    8.         While readDetails.Peek() >= 0
    9.             strResultArray(intResultArrayIndex) = readDetails.ReadLine()
    10.             intResultArrayIndex += 1
    11.         End While
    12.  
    13.         readDetails.Close()
    14.  
    15.  
    16.         Dim intSearchforTitleIndex As Integer = 0
    17.         'This loop will then iterate over the array values near the title value and then store those back into the text boxes
    18.         Do While intSearchforTitleIndex < strResultArray.Length - 1
    19.             If strResultArray(intSearchforTitleIndex) <> strRequestedBook Then
    20.                 intSearchforTitleIndex += 1
    21.             ElseIf intSearchforTitleIndex >= strResultArray.Length - 1 Then
    22.                 MessageBox.Show("Sorry, that book was not found in that text file. Please try again", "Title not found")
    23.                 If MsgBoxResult.Cancel Then Return
    24.             Else
    25.                 txtTitle.Text = strResultArray(intSearchforTitleIndex)
    26.                 txtBookID.Text = strResultArray(intSearchforTitleIndex + 1)
    27.                 txtAuthorOne.Text = strResultArray(intSearchforTitleIndex + 2)
    28.                 txtAuthorTwo.Text = strResultArray(intSearchforTitleIndex + 3)
    29.                 txtAuthorThree.Text = strResultArray(intSearchforTitleIndex + 4)
    30.                 txtPublisher.Text = strResultArray(intSearchforTitleIndex + 5)
    31.                 txtYearPublished.Text = strResultArray(intSearchforTitleIndex + 6)
    32.                 txtBuyingPrice.Text = strResultArray(intSearchforTitleIndex + 7)
    33.                 txtSellingPrice.Text = strResultArray(intSearchforTitleIndex + 8)
    34.                 txtCopyNumber.Text = strResultArray(intSearchforTitleIndex + 9)
    35.  
    36.                 If strResultArray(intSearchforTitleIndex + 10) = "True" Then
    37.                     clbGenres.SetItemChecked(0, True)
    38.                 ElseIf strResultArray(intSearchforTitleIndex + 11) = "True" Then
    39.                     clbGenres.SetItemChecked(1, True)
    40.                 ElseIf strResultArray(intSearchforTitleIndex + 12) = "True" Then
    41.                     clbGenres.SetItemChecked(2, True)
    42.                 ElseIf strResultArray(intSearchforTitleIndex + 13) = "True" Then
    43.                     clbGenres.SetItemChecked(3, True)
    44.                 ElseIf strResultArray(intSearchforTitleIndex + 14) = "True" Then
    45.                     clbGenres.SetItemChecked(4, True)
    46.                 ElseIf strResultArray(intSearchforTitleIndex + 15) = "True" Then
    47.                     clbGenres.SetItemChecked(5, True)
    48.                 ElseIf strResultArray(intSearchforTitleIndex + 16) = "True" Then
    49.                     clbGenres.SetItemChecked(6, True)
    50.                 ElseIf strResultArray(intSearchforTitleIndex + 17) = "True" Then
    51.                     clbGenres.SetItemChecked(7, True)
    52.                 ElseIf strResultArray(intSearchforTitleIndex + 18) = "True" Then
    53.                     clbGenres.SetItemChecked(8, True)
    54.                 ElseIf strResultArray(intSearchforTitleIndex + 19) = "True" Then
    55.                     clbGenres.SetItemChecked(9, True)
    56.                 End If
    57.  
    58.             End If
    59.         Loop
    60.  
    61.     End Sub

    That's the relevant part of it.

  6. #6
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Problems with getting contents from Readline() into Arrays

    Hello,

    Is this a school project?
    Is it possible to change the structure of the text file?

    Seems that if you have control over the text file a delimited version would be better meaning each line is a record rather than spread the record across multiple lines.

  7. #7
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Problems with getting contents from Readline() into Arrays

    Is there a reason you are not using the readalllines() method? This would simplify your code quite a lot.

  8. #8

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    5

    Re: Problems with getting contents from Readline() into Arrays

    Quote Originally Posted by kevininstructor View Post
    Hello,

    Is this a school project?
    Is it possible to change the structure of the text file?

    Seems that if you have control over the text file a delimited version would be better meaning each line is a record rather than spread the record across multiple lines.
    Yeah it is. I could change the structure, but I don't know how to better implement it. The code is a mess, I've been learning quite a few of the functions/methods as I've been going along, so a lot of it is stuff I've never used before now, which has left it a bit of a mess.

    Quote Originally Posted by DataMiser View Post
    Is there a reason you are not using the readalllines() method? This would simplify your code quite a lot.
    How could I implement that?

  9. #9
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Problems with getting contents from Readline() into Arrays

    Code:
    Dim fLines() As String = System.IO.File.ReadAllLines("c:\MyFile.Txt")
    This would populate an array with the lines from the file specified.

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2012
    Posts
    5

    Re: Problems with getting contents from Readline() into Arrays

    Quote Originally Posted by DataMiser View Post
    Code:
    Dim fLines() As String = System.IO.File.ReadAllLines("c:\MyFile.Txt")
    This would populate an array with the lines from the file specified.
    Thanks, I'd adjusted it so it's now:

    Code:
    1. Dim strReadDetails() As String
    2.         Dim openFileDialog As New OpenFileDialog
    3.         clearall()
    4.  
    5.         'Check to see if user would like to use last .txt file used to save info
    6.         If fileSaveDialog.FileName <> "" Then
    7.             Dim msgResponse As MsgBoxResult
    8.             MessageBox.Show("Would you like to use the previously used file?", "Access most recent text file", MessageBoxButtons.YesNo)
    9.             If msgResponse <> MsgBoxResult.Yes Then
    10.                 strReadDetails = File.ReadAllLines(openFileDialog.FileName)
    11.             Else
    12.                 openFileDialog.Filter = "Text Documents|*.txt"
    13.                 openFileDialog.Title = "Please locate the file you wish to open"
    14.                 openFileDialog.ShowDialog()
    15.                 Try
    16.                     strReadDetails = File.ReadAllLines(openFileDialog.FileName)
    17.                 Catch exFatalError As Exception
    18.                     Return
    19.                 End Try
    20.             End If
    21.         Else
    22.             openFileDialog.Filter = "Text Documents|*.txt"
    23.             openFileDialog.Title = "Please locate the file which contains the requested information"
    24.             openFileDialog.ShowDialog()
    25.             Try
    26.                 strReadDetails = File.ReadAllLines(openFileDialog.FileName)
    27.             Catch exFatalError As Exception
    28.                 Return
    29.             End Try
    30.         End If

    If I enter a word in the file, it causes the program to hang. If I enter a word not in the file, nothing at all happens (I'm assuming the exception handler is causing the last bit).

    Here's the full code for the procedure -

    http://pastebin.com/sShYzPu8

    Thanks

  11. #11
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: Problems with getting contents from Readline() into Arrays

    There are a lot of methods to read a text file into a container, search for information and present information but if you are at a learning stage for a school assignment you are better off learning the basics rather than using the best method available.

    The example below reads a text file into an array of type Book (which does not have the same information as you have as I am not here to do your homework so if you want to use this then modify as needed). The text file is structured for one record per line.

    Text file (Books.txt) resides in the same folder as the executable. You can of course use a OpenDialog to make this dynamic.
    Code:
    1,Book1,Author1,12.99
    2,Book2,Author1,5.50
    3,Book3,Author2,20.99
    Note the first column would be our identifier to the record while the remaining columns contain data we are after.

    A simple class (place in a class module) to remember data from the text file.
    Code:
    Public Class Book
        Public Property Identifier As Integer
        Public Property Name As String
        Public Property Author As String
        Public Property SellingPrice As Decimal
        Public Sub New(ByVal Identifier As Integer, ByVal BookName As String, ByVal Author As String, ByVal SellingPrice As Decimal)
            Me.Identifier = Identifier
            Me.Name = BookName
            Me.Author = Author
            Me.SellingPrice = SellingPrice
        End Sub
        Public Sub New()
        End Sub
        Public Overrides Function ToString() As String
            Return String.Format("ID: [{0}] Book: [{1}] Author: [{2}] Selling Price [{3}] ", Me.Identifier, Me.Name, Me.Author, Me.SellingPrice)
        End Function
    End Class
    Form code to work the data, TextBox controls begin with txt and button begin with cmd.
    Code:
    Public Class frmMainForm
        ''' <summary>
        ''' Oversize our array, will be shorten later with ReDim
        ''' </summary>
        ''' <remarks></remarks>
        Private BookArray(100) As Book
        Private FileName As String = IO.Path.Combine(Application.StartupPath, "Books.txt")
        Private Sub frmMainForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            LoadTextFile()
        End Sub
        Private Sub LoadTextFile()
            Dim BookData As String() = {}
            Dim Reader As New IO.StreamReader(FileName)
            Dim Line As String = ""
            Dim Index As Integer = 0
    
            Do
                Line = Reader.ReadLine()
                If Not Line Is Nothing Then
                    BookData = Line.Split(","c)
                    BookArray(Index) = New Book(CInt(BookData(0)), BookData(1), BookData(2), CDec(BookData(3)))
                    Index += 1
                End If
            Loop Until Line Is Nothing
    
            Reader.Close()
    
            ReDim Preserve BookArray(Index)
    
        End Sub
        Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
    
            If txtRequestedBook.Text.Length > 0 Then
    
                Dim FoundBook As Book = Nothing
    
                For x As Integer = 0 To BookArray.GetUpperBound(0) - 1
                    '
                    ' Case insensitive search
                    '
                    If BookArray(x).Name.ToLower = txtRequestedBook.Text.ToLower Then
                        FoundBook = BookArray(x)
                        Exit For
                    End If
                Next
    
                If FoundBook IsNot Nothing Then
                    txtBookID.Text = FoundBook.Identifier.ToString
                    txtBook.Text = FoundBook.Name
                    '
                    ' Shows ToString method of Book class
                    '
                    'MessageBox.Show(FoundBook.ToString)
                    '
                    '
                    '
                Else
                    txtBookID.Text = ""
                    txtBook.Text = ""
                    MessageBox.Show(txtRequestedBook.Text & " was not located.")
                End If
    
                ActiveControl = cmdSearch
            Else
                MessageBox.Show("Please enter a book name.")
                ActiveControl = txtRequestedBook
            End If
        End Sub
    End Class
    The above is very basic with limited exception handling.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width