Sam Dowrick
Sam Dowrick

Reputation: 21

How to get max value from the file

I'm coding the high score panel for a game I've made.

The code writes all the scores to a text file and then reads them, finds the highest score and display it in a text box. I have the issue that the largest value that displayed is never over 9.

This is the code that I've written to read the text file and write the largest value to a Label

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\samdo_000\Desktop\1234\score.txt")
topScore.Text = (fileReader.Max)

What am i doing wrong?

Thanks

scorePnl.Show()
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("C:\Users\samdo_000\Desktop\1234\score.txt", True)
file.WriteLine(pipeNumber)
file.Close()
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Users\samdo_000\Desktop\1234\score.txt")
topScore.Text = (fileReader.Max)
currentScore.Text = pipeNumber
Timer1.Enabled = False

Everytime the game ends it stores an integer (pipeNumber)

Upvotes: 1

Views: 1698

Answers (3)

T.S.
T.S.

Reputation: 19356

Assuming that you have bunch of lines and each contains a numeric value, this will do it

dim lines() as string = File.ReadAllLines("C:\Users\samdo_000\Desktop\1234\score.txt")
dim max as Long = lines.Select(function (i as Long)i).Max()
txt.Text = max

Upvotes: 0

LarsTech
LarsTech

Reputation: 81635

fileReader.Max is returning the highest valued character it can find in your string, which is probably just 9.

Assuming the text file has every score separated by a line break, you would probably just loop through each line and test to make sure your "string" is actually a score, and if so, test for the highest value:

Dim highScore As Integer = 0
For Each s As String In File.ReadAllLines("C:\Users\samdo_000\Desktop\1234\score.txt")
  Dim testScore As Integer
  If Integer.TryParse(s, testScore) Then
    highScore = Math.Max(highScore, testScore)
  End If
Next
topScore.Text = highScore.ToString

Upvotes: 1

Sam Axe
Sam Axe

Reputation: 33738

My.Computer.FileSystem.ReadAllText returns a String, which is an IEnumerable(Of Char).

The .Max() method is an extension to The IEnumerable(Of Char) - basically trading the string as an array of characters. So by using the .Max() method you are really asking for the largest single character, which amongst an array of arabic numerals will top out at "9".

You need to transform your character array into an integer array of some kind.

Assuming your file is record delimited by carriage-return/line-feed (VbCrLf), one method you could use would be:

Dim lines As List(Of String) = My.Computer.FileSystem.ReadAllText(path).Split({vbCr, vbLf}, SplitOptions.RemoveEmpty)
Dim items As List(Of Int64) = New List(Of Int64)

lines.Each(Sub(i) items.Add(Int64.Parse(i)))

Dim maxScore As Int64 = items.Max()

Code here is untested. There may be spelling/syntax errors. It's only presented as a starting point.

A much better option would be to use serialization. You should definitely read up on it. The Newtonsoft.Json serializer available from NuGet would work great - as would System.Xml.Serialization.XmlSerializer.

Upvotes: 0

Related Questions