Tony Slepian
Tony Slepian

Reputation: 53

Array.Max prints the whole array insted of the max value (C#)

My homework is to write a code of two major parts: one is to create a text file of names and numbers, and the other one is to read the same text file, and print out the biggest number out of the file. This is the part of the code that creates the text file (that I don't see any problems with, it works just fine), the class is:

class Person
{
    public string Name;
    public string Age;
}

The main is:

        Person a = new Person();
        Person b = new Person();
        Person c = new Person();
        a.Name = "Abel";
        a.Age = "20";
        b.Name = "Bob";
        b.Age = "22";
        c.Name = "Cain";
        c.Age = "25";
        string[] People = { a.Name, a.Age, b.Name, b.Age, c.Name, c.Age };

        using (StreamWriter write = new StreamWriter(@"C:\Users\A\Desktop\file check\test.txt"))
        {
            for (int i = 0; i < People.Length; i++)
            {
                write.WriteLine(People[i]);
            }
        }

The text file is very simple, and looks like this:

Abel
20
Bob
22
Cain
25

This part works ok. The part I'm having trouble with is the part where I'm supposed to read the file and print the biggest number, which looks like this:

        string PeopleCheck = @"C:\Users\A\Desktop\file check\test.txt";
        using (StreamReader read = new StreamReader(PeopleCheck))
        {
            while (true)
            {
                string FindMax = read.ReadLine();
                if (FindMax == null)
                {
                    break;
                }   
                int test;
                if(Int32.TryParse(FindMax, out test))
                {
                   // Console.WriteLine(FindMax); --> this will print all numbers, one number in each line
                    int[] numbers = FindMax.Split(' ').Select(n => Convert.ToInt32(n)).ToArray();
                    Console.WriteLine("the highest number is {0}", numbers.Max());
                }
            }
        }
    }

I used this post:Convert string to int array using LINQ , to convert the string into an array of numbers. I thought that numbers.Max() will print out the biggest number, but the output looks like this:

the highest number is 20
the highest number is 22
the highest number is 25
Press any key to continue . . .

If anyone knows how to fix this so the output will be a single number, I'll be very graful, thanks in advance for anyone who tries to help me out.

Upvotes: 1

Views: 122

Answers (1)

usr
usr

Reputation: 171206

You are reading the integers one by one, parsing them one by one and printing for each integer the current max value of the integers you have already processed.

It's a coincidence that the list comes out unchanged because the list is ordered ascendingly. If you try 3,2,1 then it will print 3,3,3.

Print after the loop once.

In the future you can resolve issues like this one using the debugger. It's very easy to see by stepping through the code.

Upvotes: 1

Related Questions