|
-
May 22nd, 2012, 10:33 AM
#1
Thread Starter
New Member
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.
-
May 22nd, 2012, 10:37 AM
#2
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.
-
May 22nd, 2012, 10:42 AM
#3
Thread Starter
New Member
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.
-
May 22nd, 2012, 10:44 AM
#4
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
-
May 22nd, 2012, 10:47 AM
#5
Thread Starter
New Member
Re: Problems with getting contents from Readline() into Arrays
vb Code:
'Variable used to locate the requested title by the user. Dim strRequestedBook As String = InputBox("Please enter the title of the requested book", "Information Needed") '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 Dim strResultArray(99) As String Dim intResultArrayIndex As Integer = 0 While readDetails.Peek() >= 0 strResultArray(intResultArrayIndex) = readDetails.ReadLine() intResultArrayIndex += 1 End While readDetails.Close() Dim intSearchforTitleIndex As Integer = 0 'This loop will then iterate over the array values near the title value and then store those back into the text boxes Do While intSearchforTitleIndex < strResultArray.Length - 1 If strResultArray(intSearchforTitleIndex) <> strRequestedBook Then intSearchforTitleIndex += 1 ElseIf intSearchforTitleIndex >= strResultArray.Length - 1 Then MessageBox.Show("Sorry, that book was not found in that text file. Please try again", "Title not found") If MsgBoxResult.Cancel Then Return Else txtTitle.Text = strResultArray(intSearchforTitleIndex) txtBookID.Text = strResultArray(intSearchforTitleIndex + 1) txtAuthorOne.Text = strResultArray(intSearchforTitleIndex + 2) txtAuthorTwo.Text = strResultArray(intSearchforTitleIndex + 3) txtAuthorThree.Text = strResultArray(intSearchforTitleIndex + 4) txtPublisher.Text = strResultArray(intSearchforTitleIndex + 5) txtYearPublished.Text = strResultArray(intSearchforTitleIndex + 6) txtBuyingPrice.Text = strResultArray(intSearchforTitleIndex + 7) txtSellingPrice.Text = strResultArray(intSearchforTitleIndex + 8) txtCopyNumber.Text = strResultArray(intSearchforTitleIndex + 9) If strResultArray(intSearchforTitleIndex + 10) = "True" Then clbGenres.SetItemChecked(0, True) ElseIf strResultArray(intSearchforTitleIndex + 11) = "True" Then clbGenres.SetItemChecked(1, True) ElseIf strResultArray(intSearchforTitleIndex + 12) = "True" Then clbGenres.SetItemChecked(2, True) ElseIf strResultArray(intSearchforTitleIndex + 13) = "True" Then clbGenres.SetItemChecked(3, True) ElseIf strResultArray(intSearchforTitleIndex + 14) = "True" Then clbGenres.SetItemChecked(4, True) ElseIf strResultArray(intSearchforTitleIndex + 15) = "True" Then clbGenres.SetItemChecked(5, True) ElseIf strResultArray(intSearchforTitleIndex + 16) = "True" Then clbGenres.SetItemChecked(6, True) ElseIf strResultArray(intSearchforTitleIndex + 17) = "True" Then clbGenres.SetItemChecked(7, True) ElseIf strResultArray(intSearchforTitleIndex + 18) = "True" Then clbGenres.SetItemChecked(8, True) ElseIf strResultArray(intSearchforTitleIndex + 19) = "True" Then clbGenres.SetItemChecked(9, True) End If End If Loop End Sub
That's the relevant part of it.
-
May 22nd, 2012, 11:30 AM
#6
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.
-
May 22nd, 2012, 01:26 PM
#7
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.
-
May 22nd, 2012, 07:54 PM
#8
Thread Starter
New Member
Re: Problems with getting contents from Readline() into Arrays
 Originally Posted by kevininstructor
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.
 Originally Posted by DataMiser
Is there a reason you are not using the readalllines() method? This would simplify your code quite a lot.
How could I implement that?
-
May 22nd, 2012, 08:53 PM
#9
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.
-
May 22nd, 2012, 11:47 PM
#10
Thread Starter
New Member
Re: Problems with getting contents from Readline() into Arrays
 Originally Posted by DataMiser
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:
Dim strReadDetails() As String
Dim openFileDialog As New OpenFileDialog
clearall()
'Check to see if user would like to use last .txt file used to save info
If fileSaveDialog.FileName <> "" Then
Dim msgResponse As MsgBoxResult
MessageBox.Show("Would you like to use the previously used file?", "Access most recent text file", MessageBoxButtons.YesNo)
If msgResponse <> MsgBoxResult.Yes Then
strReadDetails = File.ReadAllLines(openFileDialog.FileName)
Else
openFileDialog.Filter = "Text Documents|*.txt"
openFileDialog.Title = "Please locate the file you wish to open"
openFileDialog.ShowDialog()
Try
strReadDetails = File.ReadAllLines(openFileDialog.FileName)
Catch exFatalError As Exception
Return
End Try
End If
Else
openFileDialog.Filter = "Text Documents|*.txt"
openFileDialog.Title = "Please locate the file which contains the requested information"
openFileDialog.ShowDialog()
Try
strReadDetails = File.ReadAllLines(openFileDialog.FileName)
Catch exFatalError As Exception
Return
End Try
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
-
May 23rd, 2012, 08:47 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|