1

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.

1 Answers1

1

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.

usr
  • 168,620
  • 35
  • 240
  • 369
  • I'm not sure I understand what you mean... you said to print once out of the loop, but the array doesn't exist outside of it. I noticed with the debugger what my problem is, but I honestly dont know how to fix it... – Tony Slepian Sep 19 '18 at 14:29
  • @TonySlepian can you post an example text file please. Is that question still relevant for you after that time? I only now notice 2 weeks have passed. – usr Sep 19 '18 at 14:34
  • It's still relevant, and I'll add the text file to the main question – Tony Slepian Sep 19 '18 at 14:58
  • The collection should be outside the loop (I had not noticed this issue). Try a `List` and fill it with each pair of lines that you parse. – usr Sep 19 '18 at 15:15
  • I'm not sure how to do this, but I'll ask my teacher how to write your method, thank you for your help ^^ – Tony Slepian Sep 19 '18 at 15:25