Searching sequential file loading into array vb.net and searching - arrays

I'm using VB.NET 2010 and have been trying to wrap my head around implementing a search feature using a sequential file. Originally I was trying to use a sequential file to read from and search but that has proven to be extremely difficult so I've taken a simple approach using two different arrays with data and try to search through Artist and Album.
The code below takes input from txtSearch.Text which will be for search by artist. I have a duplicate artist "TeeBee" but when I search for that artist I only receive one result instead of two as there are two different albums under the artist "TeeBee".
I thought about adding another loop but it doesn't work. I also thought that the results were getting cut off as there isn't a way to add a return to the results to continue on.
I'm a beginner to programming so please keep that in mind.
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
' Artist array
Dim strArtist() As String = {"Dillinja", "TeeBee", "Dieselboy", "TeeBee"}
' Album array
Dim strAlbum() As String = {"Untitled", "Scorpion", "Horns", "Blades"}
Dim strSearchForArtist As String
Dim intSub As Integer
' artist search
strSearchForArtist = txtSearch.Text
Do Until intSub = strAlbum.Length OrElse
strSearchForArtist = strArtist(intSub)
intSub = intSub + 1
Loop
If intSub < strArtist.Length Then
lstLibrary.Items.Add(strArtist(intSub) & " " & strAlbum(intSub) & vbNewLine)
Else
MessageBox.Show("Invalid", "Bad", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Attempt to load text file into array but it doesn't create separate arrays like album, artist genre. - 11-19-14
Private Sub btnLoadArray_Click(sender As Object, e As EventArgs) Handles btnLoadArray.Click
Dim filePath As String = "library.txt"
Dim sr As IO.StreamReader
sr = IO.File.OpenText(filePath)
' look inside file and read every line
' this will be how we put the number for
' our array below
Dim TotalLines As Integer = 0
Dim word As String = ""
' need 3 to be dynamic so we get all lines in the file to build the array words(#)
' wanted to use words(,) but that does not work
Dim words() As String = IO.File.ReadAllLines(filePath)
Dim i As Integer = 0
' when the peak value is -1 we're at the end of the file
Do Until sr.Peek = -1
' load one at a time
word = sr.ReadLine()
' load word into array
words(i) = word
' output
lstArtist.Items.Add(words(i))
' increment counter
i += 1
Loop
'close the file
sr.Close()
End Sub

Well, instead of using arrays and handle all the complications I really suggest you to have a more object oriented approach
So start defining a class to keep the info for an Album like this
Public Class Album
Public ArtistName as String
Public AlbumTitle as String
Public Function ToString() as String
return ArtistName & " - " & AlbumTitle
End Function
End Class
Now you could remove the array mess using a List(Of Album) and asking to each item of this list to render its content via the ToString method.
Also an important role here is reserved to the IEnumerable function Where that extract from the List(Of Album) all the elements that respects the Lambda expression required by the Where method.
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
' Here the list is fixed but you can easily build it loading data from a database
' or from some other storage medium like a file etc...
Dim albumList = new List(Of Album) From _
{
new Album With { .ArtistName = "Dillinja", .AlbumTitle = "Untitled" },
new Album With { .ArtistName = "TeeBee", .AlbumTitle = "Scorpion"},
new Album With { .ArtistName = "Dieselboy", .AlbumTitle = "Horns" },
new Album With { .ArtistName = "TeeBee", .AlbumTitle = "Blades" }
}
' To help search you could integrate the ToLower expressions in
' Plutonix answer here...
Dim searchTerm = txtSearch.Text
Dim searchResult = albumList.Where(Function(x) x.ArtistName = searchTerm)
lstLibrary.Items.Clear()
if searchResult.Count > 0 Then
For Each item in searchResult
lstLibrary.Items.Add(item.ToString())
Next
Else
MessageBox.Show("Not found")
End if
End Sub

Rather than arrays, a List(of String) is easier to manage and provides more efficiency and the kind of functions you probably want.
Private Artists As New List(of String)
....
Artists.Addrange({"Dillinja", "TeeBee", "Dieselboy", "TeeBee"})
...
' find a single item:
If Artists.Contains(txtSearch.Text) Then ' no looping required
' a dupe
Else
' not a dupe
End If
To get all the matching items:
Dim find = Artists.Where(Function(s) _
s.ToLowerInvariant = txtSearch.Text.ToLowerInvariant).ToList
The resulting find will also be a List(of String) containing the matching item. This time it is case insensitive. To get the count of dupes:
Dim finds = Artists.LongCount(Function(n) n.ToLowerInvariant = _
txtSearch.Text.ToLowerInvariant.ToLowerInvariant)
I dont know how much value either can be. Neither 2 nor a List of "Teebee", "TeeBee" is very useful or interesting. More typically, you want the return to be the entire object (like an album) associated with the search term. This requires a Class which glues Arist, Album, Genere, Tracks etc together though (see Steve's answer for a start on this part).
As for the whole file part, the list(s) can be easily serialized allowing an entire list to be saved or reloaded from disk in 2-3 lines of code.

Related

(VB.NET) display the lower half of a textfile to a listbox

I have to make a application that organizes a list of runners and their teams. In the following text file, I have to remove the top half of the text file (the top half being the listed teams) and display only the bottom half (the runners)in a listbox item.
The Text file:
# School [School Code|School Name|Coach F-Name|Coach L-Name|AD F-Name|AD L Name]
WSHS|Worcester South High School|Glenn|Clauss|Bret|Zane
WDHS|Worcester Dorehty High School|Ellsworth|Quackenbush|Bert|Coco
WBCHS|Worcester Burncoat High School|Gail|Cain|Kevin|Kane
QRHS|Quabbin Regional High School|Bob|Desilets|Seth|Desilets
GHS|Gardner High School|Jack|Smith|George|Fanning
NBHS|North Brookfield High School|Hughe|Fitch|Richard|Carey
WHS|Winchendon High School|Bill|Nice|Sam|Adams
AUBHS|Auburn High School|Katie|Right|Alice|Wonderland
OXHS|Oxford High School|Mary|Cousin|Frank|Daughter
# Roster [Bib #|School Code|Runner's F-Name|Runner's L-Name]
101|WSHS|Sanora|Hibshman
102|WSHS|Bridgette|Moffitt
103|WSHS|Karine|Chunn
104|WSHS|Shanita|Wind
105|WSHS|Fernanda|Parsell
106|WSHS|Albertha|Baringer
107|WSHS|Carlee|Sowards
108|WDHS|Maisha|Kleis
109|WDHS|Lezlie|Berson
110|WDHS|Deane|Rocheleau
111|WDHS|Hang|Hodapp
112|WDHS|Zola|Dorrough
113|WDHS|Shalon|Mcmonigle
I have some code that reads each row from the text file as an array and uses boolean variables to determine where to end the text file. This worked with displaying only the teams, which I've managed to do. But I now need to do the opposite and display only the players, and I'm a bit stumped.
My Code:
Private Sub btnLoadTeams_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadTeam.Click
' This routine loads the lstTeam box from an ASCII .txt file
' # School [School Code | Name | Coach F-Name| Coach L-Name | AD F-Name | AD L-Name]
Dim strRow As String
Dim bolFoundCode As Boolean = False
Dim bolEndCode As Boolean = False
Dim bolFoundDup As Boolean = False
Dim intPosition As Integer
Dim intPosition2 As Integer
Dim strTeamCodeIn As String
Dim textIn As New StreamReader( _
New FileStream(txtFilePath.Text, FileMode.OpenOrCreate, FileAccess.Read))
' Clear Team listbox
lstTeam.Items.Clear()
btnDeleteRunner.Enabled = True
Do While textIn.Peek <> -1 And Not bolEndCode
Me.Refresh()
strRow = textIn.ReadLine.Trim
If Not bolFoundCode Then
If "# SCHOOL " = UCase(Mid(strRow, 1, 9)) Then
bolFoundCode = True
End If
Else
If Mid(strRow, 1, 2) <> "# " Then
For Each item As String In lstTeam.Items
intPosition = InStr(1, strRow, "|")
strTeamCodeIn = Mid(strRow, 1, intPosition - 1)
intPosition2 = InStr(1, item, strTeamCodeIn)
If intPosition2 > 0 Then
bolFoundDup = True
MsgBox("Found Duplicate School Code: " & strTeamCodeIn)
End If
Else
bolEndCode = True
Next
If Not bolFoundDup Then
lstTeam.Items.Add(strRow)
Else
lstTeam.Items.Add("DUPLICATE School Code: " & strRow)
lstTeam.Items.Add("Please correct input file and reload teams")
bolEndCode = True
End If
End If
End If
Loop
End Sub
Ive put bolEndCode = True in between the part that reads the mid section of the text file, but all Ive managed to display is the following in the listbox:
# Roster [Bib #|School Code|Runner's F-Name|Runner's L-Name]
Any help or hints on how I would display just the runners to my "lstPlayers" listbox would be greatly appreciated. I'm a beginner programmer and We've only just started learning about reading and writing arrays in my .NET class.
First I made 2 classes, one Runner and one School. These have the properties available in the text file. As part of the class I added a function that overrides .ToString. This is for he list boxes that call .ToString for display.
Next I made a function that reads all the data in the file. This is very simple with the File.ReadLines method.
Then I created 2 variables List(Of T) T stands for Type. Ours Types are Runner and School. I used List(Of T) instead of arrays because I don't have to worry about what the size of the list is. No ReDim Preserve, just keep adding items. The FillList method adds the data to the lists. First I had to find where the schools ended and the runners started. I used the Array.FindIndex method which is a bit different because the second parameter is a predicate. Check it out a bit. Now we know the indexes of the lines we want to use for each list and use a For...Next loop. In each loop an instance of the class is created and the properties set. Finally the new object is added to the the list.
Finally we fill the list boxes with a simple .AddRange and the lists.ToArray. Note that we are adding the entire object, properties and all. The neat thing is we can access the properties from the listbox items. Check out the SelectedIndexChanged event. You can do the same thing with the Runner list box.
Sorry, I couldn't just work with your code. I have all but forgotten the old vb6 methods. InStr, Mid etc. It is better when you can to use .net methods. It makes your code more portable when the boss says "Rewrite the whole application in C#"
Public Class Runner
Public Property BibNum As Integer
Public Property SchoolCode As String
Public Property FirstName As String
Public Property LastName As String
Public Overrides Function ToString() As String
'The listbox will call .ToString when we add a Runner object to determin what to display
Return $"{FirstName} {LastName}" 'or $"{LastName}, {FirstName}"
End Function
End Class
Public Class School
Public Property Code As String
Public Property Name As String
Public Property CoachFName As String
Public Property CoachLName As String
Public Property ADFName As String
Public Property ADLName As String
'The listbox will call .ToString when we add a School object to determin what to display
Public Overrides Function ToString() As String
Return Name
End Function
End Class
Private Runners As New List(Of Runner)
Private Schools As New List(Of School)
Private Function ReadData(path As String) As String()
Dim lines = File.ReadLines(path).ToArray
Return lines
End Function
Private Sub FillLists(data As String())
Dim location = Array.FindIndex(data, AddressOf FindRosterLine)
'The first line is the title so we don't start at zero
For index = 1 To location - 1
Dim SplitData = data(index).Split("|"c)
Dim Schl As New School
Schl.Code = SplitData(0)
Schl.Name = SplitData(1)
Schl.CoachFName = SplitData(2)
Schl.CoachLName = SplitData(3)
Schl.ADFName = SplitData(4)
Schl.ADLName = SplitData(5)
Schools.Add(Schl)
Next
For index = location + 1 To data.GetUpperBound(0)
Dim SplitData = data(index).Split("|"c)
Dim Run As New Runner
Run.BibNum = CInt(SplitData(0))
Run.SchoolCode = SplitData(1)
Run.FirstName = SplitData(2)
Run.LastName = SplitData(3)
Runners.Add(Run)
Next
End Sub
Private Function FindRosterLine(s As String) As Boolean
If s.Trim.StartsWith("# Roster") Then
Return True
Else
Return False
End If
End Function
Private Sub FillListBoxes()
Dim arrRunners As Runner() = Runners.ToArray
Dim arrSchools As School() = Schools.ToArray
ListBox1.Items.AddRange(arrSchools)
ListBox2.Items.AddRange(arrRunners)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim arrRunner = ReadData("Runners.txt")
FillLists(arrRunner)
FillListBoxes()
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim Schl = DirectCast(ListBox1.SelectedItem, School)
TextBox1.Text = Schl.CoachLName
TextBox2.Text = Schl.Code
End Sub

Adding Item to Array (VB 2008)

The objective of the program is to interpret hockey statistics from a file using StreamReader and then display an added column of points. The following code kinda does so, however it’s ineffective in the sense that it doesn’t add the points value to the array - it separately outputs it. Looking for assistance as to how it would be possible to incorporate the points value into aryTextFile();
Dim hockeyFile, LineOfText, aryTextFile() As String
Dim i As Integer
Dim nameText(), NumberText(), goalsText(), assistsText(), GamesWonText() As String
Dim IntAssists(), IntGoals(), PointsText() As Single
hockeyFile = "C:\Users\Bob\Downloads\hockey.txt" 'state location of file
Dim objReader As New System.IO.StreamReader(hockeyFile) 'objReader can read hockeyFile
For i = 0 To objReader.Peek() <> -1 'reads each line seperately, ends when there is no more data to read
LineOfText = objReader.ReadLine 'stores seperate lines of data in HockeyFile into LineofText
aryTextFile = LineOfText.Split(",") 'takes lines and converts data into array
Name = aryTextFile(0) 'first piece of data in lines of text is the name
nameText(i) = aryTextFile(0)
If nameText(0) = "Name" Then
TextBox1.Text = LineOfText & ", Points." & vbCrLf 'displays first line fo text and adds "Points" label
End If
If Name <> "Name" Then 'when second line starts, then begin to intepret data
NumberText(i) = aryTextFile(1)
assistsText(i) = aryTextFile(2) 'assists are in third value of array
goalsText(i) = aryTextFile(3) 'goals are in fourth value of array
GamesWonText(i) = aryTextFile(4)
IntAssists(i) = Val(assistsText(i)) 'since our assists value is a string by default, it must be converted to a integer
IntGoals(i) = Val(goalsText(i)) 'since our goals value is a string by default, it must be converted to a integer
PointsText(i) = (IntGoals(i) * 2) + (IntAssists(i)) 'goals are two points, assists are one point
TextBox1.Text = TextBox1.Text & NumberText(i) & assistsText(i) & goalsText(i) & GamesWonText(i) & PointsText(i) & vbCrLf 'Displays points as last value in each line
End If
Next i
This should get you pretty close:
It'll need extra validation. It doesn't take into account whatever value you have between the name and the goals.
Private Sub ProcessHockeyStats()
Try
Dim inputFile As String = "c:\temp\hockey.txt"
Dim outputFile As String = "c:\temp\output.txt"
If Not File.Exists(inputFile) Then
MessageBox.Show("Missing input file")
Return
End If
If File.Exists(outputFile) Then
File.Delete(outputFile)
End If
Dim lines() As String = File.ReadAllLines(inputFile)
Dim output As List(Of String) = New List(Of String)
Dim firstLine As Boolean = True
For Each line As String In lines
Dim values() As String = line.Split(","c)
Dim points As Integer
If firstLine Then
output.Add("Name, Assists, Goals, Points")
firstLine = False
Else
'needs validation for values
points = CInt(values(1) * 2) + CInt(values(2))
output.Add(String.Concat(line, ",", points))
End If
Next
File.WriteAllLines("c:\temp\outfile.txt", output)
Catch ex As Exception
MessageBox.Show(String.Concat("Error occurred: ", ex.Message))
End Try
End Sub
VS2008 is ancient, especially when later versions of Visual Studio are free. I felt like showing an implementation using more-recent code. Like others, I strongly support building a class for this. The difference is my class is a little smarter, using the Factory pattern for creating instances and a Property to compute Points as needed:
Public Class HockeyPlayer
Public Property Name As String
Public Property Number As String
Public Property Assists As Integer
Public Property Goals As Integer
Public Property Wins As Integer
Public ReadOnly Property Points As Integer
Get
Return (Goals * 2) + Assists
End Get
End Property
Public Shared Function FromCSVLine(line As String) As HockeyPlayer
Dim parts() As String = line.Split(",")
Return New HockeyPlayer With {
.Name = parts(0),
.Number = parts(1),
.Assists = CInt(parts(2)),
.Goals = CInt(parts(3)),
.Wins = CInt(parts(4))
}
End Function
End Class
Dim hockeyFile As String = "C:\Users\Bob\Downloads\hockey.txt"
Dim players = File.ReadLines(hockeyFile).Skip(1).
Select(Function(line) HockeyPlayer.FromCSVLine(line)).
ToList() 'ToList() is optional, but I included it since you asked about an array
Dim result As New StringBuilder("Name, Number, Assists, Goals, Wins, Points")
For Each player In players
result.AppendLine($"{player.Name}, {player.Number}, {player.Assists}, {player.Goals}, {player.Wins}, {player.Points}")
Next player
TextBox1.Text = result.ToString()
I was gonna give you VS 2008 version afterward, but looking at this, the only thing here you couldn't do already even by VS 2010 was string interpolation... you really should upgrade.
Parallel arrays are really not the way to handle this. Create a class or structure to organize the data. Then create a list of the class. The list can be set as the DataSource of a DataGridView which will display your data in nice columns with headings matching the names of your properties in the Hockey class. You can easily order your data in the HockeyList by any of the properties of Hockey.
Public Class Hockey
Public Property Name As String
Public Property Number As String
Public Property Goals As Integer
Public Property Assists As Integer
Public Property Points As Integer
Public Property GamesWon As Integer
End Class
Private HockeyList As New List(Of Hockey)
Private Sub FillListAndDisplay()
Dim path = "C:\Users\Bob\Downloads\hockey.txt"
Dim Lines() = File.ReadAllLines(path)
For Each line As String In Lines
Dim arr() = line.Split(","c)
Dim h As New Hockey()
h.Name = arr(0)
h.Number = arr(1)
h.Assists = CInt(arr(2).Trim)
h.Goals = CInt(arr(3).Trim)
h.GamesWon = CInt(arr(4).Trim)
h.Points = h.Goals * 2 + h.Assists
HockeyList.Add(h)
Next
Dim orderedList = (From scorer In HockeyList Order By scorer.Points Ascending Select scorer).ToList
DataGridView1.DataSource = orderedList
End Sub

Loading file into array then sorting from highest to lowest

I am trying to order the largest scores in a file that is loaded into an array. Currently, the program opens the file, then reads it and then splits up each line into two parts - a name and a score; then stores that in an array. I am not sure how I can sort the array to find the largest 10 scores and put that into a listbox. At the moment, the program finds any scores above 0 and puts it in the listbox
Dim FileNum As Integer = FreeFile()
FileOpen(FileNum, "GameResultsFile", OpenMode.Input)
For index = 0 To 99
Dim temp() As String = LineInput(FileNum).Split(",") 'CUTTING LINE INTO TWO SECTIONS
MemoryGame.HighScores(index).Name = temp(0) 'NAME (First Part of Line)
MemoryGame.HighScores(index).Score = temp(1) 'SCORE (Second Part of Line)
If temp(1) > 0 Then 'If any of the scores is above 0 then
ListBox1.Items.Add(temp(0) + " " + temp(1)) ' display the name of the person who got that score and their score
End If
Next
FileClose()
Comments and explainations in line
'Note Imports System.IO
Structure Player
Public Score As Integer
Public Name As String
'Added a constructor to the structure to make it easy to add new Player
Public Sub New(myScore As Integer, myName As String)
Score = myScore
Name = myName
End Sub
End Structure
Private HighScores(99) As Player
Private index As Integer 'used in both LoadArray and SortAndDisplayArray
Private Sub LoadArray()
Using sr As New StreamReader("GameResultsFile.txt")
Dim line As String
Do While sr.Peek() > -1 'Peek checks if there is another character in the file
line = sr.ReadLine()
Dim temp() As String = line.Split(","c) 'CUTTING LINE INTO TWO SECTIONS
'Notice the elements of the temp array are switched to match the
'Player constructor (Sub New)
HighScores(index) = New Player(CInt(temp(1)), temp(0))
index += 1 'not only keeps track of the index but remembers how many elements
'we have added to HighScores
Loop
End Using
End Sub
Private Sub SortAndDisplayArray()
'This is the LINQ way to do it, you can do a great deal in one line of code
'There is a loop underneath but you don't have to write it.
'I added a Take clause so we will not get a bunch of 0- in the list box going up to index 99
' You might want to show, for example only the top ten scorers, so change to Take 10
Dim orderArray = From scorer In HighScores Order By scorer.Score Descending Select $"{scorer.Score} - {scorer.Name}" Take index
ListBox1.DataSource = orderArray.ToList
End Sub
I still think a List(Of T) would be easier but I have a feeling your assignment requires you to use an array.
I'd do it something like this using IComparable
First I would load the data from your text file like so and save the data to a List Of and the Type would be the Player class down below.
'' Get Data From Text File And Create An Anonymous Type
Private Sub LoadPlayerAndScore(path As String)
'' Load data from text file
Dim data = From line In System.IO.File.ReadAllLines(path)
Let val = line.Split(",")
Select New With {Key .Name = val(0), Key .Score = val(1)}
'' Save data to list
For Each pair In data
Dim player As New Player With {
.Name = pair.Name,
.Score = pair.Score
}
playersList.Add(player)
Next
End Sub
I would then go on to create a player class which will Implement the ICompareble listed above.
Class Player
Implements IComparable(Of Player)
Public Property Name As String
Public Property Score As Integer
Public Sub Player(ByVal name As String, ByVal score As Integer)
Me.Name = name
Me.Score = score
End Sub
'' Sort Player From The Hightest Score To The Lowest Score
Private Function IComparable_CompareTo(other As Player) As Integer Implements IComparable(Of Player).CompareTo
Return other.Score.CompareTo(Me.Score)
End Function
End Class
I would then create some public variables such as
Dim playersList As New List(Of Player)
Dim fileLocation As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Settings.txt")
Change the path to your file's location.And finally in a Form Load Event I would call it all like this
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'' Load Data From Text File On Desktop
LoadPlayerAndScore(path:=fileLocation)
'' Sort List
playersList.Sort()
'' Add Values To List
For Each p As Player In playersList
ListBox1.Items.Add("Name: " + p.Name + " Score: " + p.Score.ToString())
Next
End Sub
Here is what the code should look something like altogether
Public Class Form1
Dim playersList As New List(Of Player)
Dim fileLocation As String = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Settings.txt")
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
'' Load Data From Text File On Desktop
LoadPlayerAndScore(path:=fileLocation)
'' Sort List
playersList.Sort()
'' Add Values To List
For Each p As Player In playersList
ListBox1.Items.Add("Name: " + p.Name + " Score: " + p.Score.ToString())
Next
End Sub
'' Get Data From Text File And Create An Anonymous Type
Private Sub LoadPlayerAndScore(path As String)
'' Load data from text file
Dim data = From line In System.IO.File.ReadAllLines(path)
Let val = line.Split(",")
Select New With {Key .Name = val(0), Key .Score = val(1)}
'' Save data to list
For Each pair In data
Dim player As New Player With {
.Name = pair.Name,
.Score = pair.Score
}
playersList.Add(player)
Next
End Sub
End Class
Class Player
Implements IComparable(Of Player)
Public Property Name As String
Public Property Score As Integer
Public Sub Player(ByVal name As String, ByVal score As Integer)
Me.Name = name
Me.Score = score
End Sub
'' Sort Player From The Hightest Score To The Lowest Score
Private Function IComparable_CompareTo(other As Player) As Integer Implements IComparable(Of Player).CompareTo
Return other.Score.CompareTo(Me.Score)
End Function
End Class
Here is the output I gotAnd here is what the text files data looked like.
ANDREW,25
MERVE,12
RUZGAR,50
And for the top ten people, follow the comments above.
How about try this?
Dim sortedArray = _
File _
.ReadAllLines("GameResultsFile") _
.Select(Function (line) line.Split(","c)) _
.Select(Function (parts) New With { .Name = parts(0), .Score = Integer.Parse(parts(1)) }) _
.OrderByDescending(Function (x) x.Score) _
.Select(Function (x) x.Name & " " & x.Score)
.ToArray()
For Each item As String In sortedArray
ListBox1.Items.Add(item)
Next

Why Won't My Array Sort? (Visual Basic)

I'm working on a project, for school, that reads text from a .txt file to an array. After doing that, I'm supposed to sort the array, alphabetically, and then list the contents in a listbox. Here is my code:
Imports System.IO
Public Class Form1
'Allow array to be accessed by the entire program
Public books(1) As String
Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
'Declare variables
Dim sr As New StreamReader("C:\Users\Bryson\Desktop\BooksinStock.txt")
Dim book As String
Dim i As Integer = 0
'Establish loop to read contents of the text file into the array and list the array in the listbox
'the sr.Peek = -1 simply means that the reader has reached the end of the file and there is nothing more to be read
Do Until sr.Peek = -1
book = sr.ReadLine()
'ReDim allows the array to grow with the set amount of books in text file
ReDim books(books.Length + 1)
books(i) = book
i += 1
Loop
Array.Sort(books)
lstBoxInventory.Items.Add(books(i))
End Sub
End Class
However, when I run the program, I receive an error on the lstBoxInventory.Items.Add(books(i)) line that says "an unhandled exception of the type 'System.ArgumentNullException' occurred in System.Windows.Forms.Dll
I've tried to lay the code out in various ways to get the sort to work but keep coming up short. Does anyone know how to get rid of this null error?
The problem happening because "i" is larger than the highest index
Do Until sr.Peek = -1
book = sr.ReadLine()
ReDim books(books.Length + 1)
books(i) = book
i += 1 'This is adding 1 to the very end
Loop
Array.Sort(books)
lstBoxInventory.Items.Add(books(i)) 'When the items are being added it is trying to add an extra i that does not exist
edit
Honestly I would change the format to use ReadAllLines and list(of String)
Something like (Im writing code from memory)
Dim bookList as new List(of String)
Dim bookTextFile as String() = File.ReadAllLines("C:\booklist.txt")
for each book as String in bookTextFile
bookList.add(book)
next
bookList.Sort
Edit Again
Just using this
Dim bookList As String() = System.IO.File.ReadAllLines("C:\Users\Bryson\Desktop\BooksinStock.txt")
creates a single dimension array ..
Strings() are single Arrays String(,) are two dimensional Arrays
Honestly your whole homework could be
Dim bookList As String() = System.IO.File.ReadAllLines("C:\Users\Bryson\Desktop\BooksinStock.txt")
Array.Sort(BookList)
boom - done.
Test it using
for each book as String in BookList
Msgbox(book)
next
You could do
Dim bookList As String() = System.IO.File.ReadAllLines("C:\Users\Bryson\Desktop\BooksinStock.txt")
Dim books(bookList.Length - 1) As String 'This is the same as ReDim
For x As Integer = 0 To bookList.Length - 1
books(x) = bookList(x)
Next
Array.Sort(books)
but you would literally be saying bookList = books
But.... if you just want to get your code working, just try this
lstBoxInventory.Items.Add(books(i -1)) 'This takes away the extra i that you added

Need help getting array to work with string builder

So I am building a stock info retrieval program, and I have a good bit of it done so far. Right now I have it return the price from the Yahoo Finance API by singling out the price in the returned API data and then shove it in a listbox with the stock symbol and price. That part works great, but I want to take it a step further now and be able to do what I want with other parts of that returned data. The normal format for the data is:
"<symbol>", <price>, "<date>", "<time>", etc.
If you take a look at my code, right now I have a Getstockinfo function that retrieves the full API output and converts each object(symbol, price, etc) into a new stringbuilder line, and then separates the lines by the commas using the ModifyLine function.
I want to now get the GetStockInfo to return an array, which would allow me to use that array as I pleased outside of that function(in eventhandlers, etc.)
ANY help would be much appreciated!
Public Function GetStockInfo(ByVal pstrSymbol As String) As String
Dim strURL As String
Dim strApiOutput As String
'Yahoo API
strURL = "http://quote.yahoo.com/d/quotes.csv?" & _
"s=" & pstrSymbol & _
"&d=t" & _
"&f=sl1d1t1c1ohgvj1pp2wern"
strApiOutput = RequestWebData(strURL)
'Create stringbuilder(easier to append, replace and insert data)
Dim strReturn As New System.Text.StringBuilder
'Seperate API output into different lines by using LineFeed
For Each strLine As String In strApiOutput.Split(ControlChars.Lf)
'makes sure line actually exists, if so, seperate and add to array using ModifyLine function
If strLine.Length > 0 Then
strReturn.Append(ModifyLIne(strLine) & Environment.NewLine)
End If
Next
Return strReturn.ToString
End Function
Private Function ModifyLIne(ByVal strLine As String) As String
Dim arrLine() As String
'Splits lines by the commas(the normal api output is as follows: <Obj1>, <Obj2>, <Obj3>, etc
'with Intermediary quotes here and there for certain objects such as Company Name
arrLine = strLine.Split(","c)
decStockPrice = CDec(arrLine(1))
Return decStockPrice.ToString
End Function
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim strSymbol As String = txtSymbol.Text
lstStocks.Items.Add(strSymbol.ToUpper() & " - " & GetStockInfo(strSymbol))
End Sub
I want to take it a step further now and be able to do what I want with other parts of that returned data This will do what you want, just not how you want (arrays are old-school)
Class StockQuote
Public Symbol As String
Public Price As Decimal
Public QuoteDate As DateTime
' etc
End Class
Friend myQuotes As New List(of StockQuote)
Then after you get a quote and determine you want to save it (are you saving all of them?):
Dim SQ as New StockQuote
With SQ
.Symbol = arrLine(0) ' so you can pass an array or list of ticker syms
.Price = Convert.ToDecimal(arrLine(1))
' not much sense saving Time and Date as sep items
' if you are getting multiple quotes for a day (vs ending)
.QuoteDate = combine arrline(2) and arrLine(3) ?
etc
End SQ
myQuotes.Add(SQ)
Now, myQuotes will have a list of all the quotes for all the symbols. Get them like you populated it: txtSym.Text = myQuotes(N).Symbol

Resources