user2897499
user2897499

Reputation: 49

getting highest number on a textfile

So i have this kind of a simple snake game and i am willing to have a label with a text: "High score" And then another label after it with the highest number of score it can find from a textfile.

string lines = " \r\n" + SnkScore.Text;            
File.AppendAllText("C:\\Users\\" + System.Environment.UserName +"\\Documents\\Data\\Scrn.txt", lines);

HghScore.Text = lines; 

"Scrn.txt" is a text file which contains all the points that user has ever got in the snake game.

"SnkScore" is a label that has the current amount of points, and the "HghScore" is the highscore label where i am willing to load the highest value of "Scrn.txt" which has all the points user has ever got. So restarts it would show the highest value of the textfile on the "HghScore" label.

Upvotes: 0

Views: 3339

Answers (3)

Dan Stevens
Dan Stevens

Reputation: 6830

Assuming that the score.txt file will never contain an invalid score, you canuse LINQ (make sure that there's a using System.Linq; statement):

int maxScore = File.ReadAllLines(@"C:\score.txt")
                   .Select(s => int.Parse(s))
                   .Max();

If you want to make your program more robust, the following program will convert invalid lines to 0:

using System;
using System.Linq;

namespace HighScore
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] scores = {
                "78",
                "98",
                "88",
                "77",
                "Bad score",
                "124",
                "3",
                "678",
                "4",
                "123",
                "456",       
            };
            //scores = File.ReadAllLines(@"C:\score.txt");

            int maxScore = scores.Select(line => {
                int score = 0;
                int.TryParse(line, out score);
                return score;
            }).Max();

            Console.WriteLine("Maximum score is {0}", maxScore);
            Console.ReadKey();
        }
    }
}

Upvotes: 2

user3096476
user3096476

Reputation: 232

If you want to use Linq:

To get just one max number: solution 1:

var max=File.ReadAllLines(@"C:\score.txt").Max(m=> m.ToInt());

solution 2:

 var max=File.ReadAllLines(@"C:\score.txt").Select(int.Parse).Max();

If you want a bunch of them then sort it and Take x number from top:

var topFive=File.ReadAllLines(@"C:\score.txt").Select(int.Parse).OrderByDescending(m=> m).Take(5);

Personally, I would use TryParse in this scenario though, so manually going through each line and "TryParsing" this would be better.

Upvotes: 2

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

Solution : assuming that each score is in New-Line.

       static void Main(string[] args)
        {
            String [] lines= File.ReadAllLines(@"C:\score.txt");
            long max = 0;
            long score=0;
            foreach (String line in lines)
            {
                if (Int64.TryParse(line, out score))
                {
                if (score > max)
                    max = score;
                }
            }
            Console.WriteLine("Maximum Score is "+max);
        }

Input File : c:\score.txt with following scores:

78
98
88
77
124
3
678
4
123
456

Output :

Maximum Score is 678

Upvotes: 1

Related Questions