Home > database >  Int.tryparse, when inout is a string, will still fill up an array slot
Int.tryparse, when inout is a string, will still fill up an array slot

Time:10-25

So im following a tutorial and I wanted to mess around with the code myself and use a Int.Tryparse instead of a Int.Parse.

It worked but when I enter a character or string it will still fill up a slot in my array as a "0".

Here's my code:

Console.WriteLine("How many employee IDs would you like to register?");



string input = Console.ReadLine();

int length; 

if(int.TryParse(input, out length))
{
    int[] employeeIds = new int[length];

    for (int i = 0; i < length; i  )
    {
        Console.Write("Please enter the employee ID.");
        string input2 = Console.ReadLine();
        int id;
        if(int.TryParse(input2, out id))
        {
            employeeIds[i] = id;
        }
        else
        {
            Console.WriteLine("Invalid input, please enter a valid integer number.");
        }
    }
    for (int i = 0; i < employeeIds.Length; i  )
    {
        Console.WriteLine($"ID {i   1}:  \t{employeeIds[i]}");
    }
}
else
{
    Console.WriteLine("Input is invalid, please input an integer number.");
}

I would like to learn how to prevent it from filling up an array slot with a 0 and only add to it if the input was valid ( an int).

Any help would be greatly appreciated thank you!

CodePudding user response:

The problem is that when parsing fails (i.e. non-parsable string), the iterator (which represents array position) is incremented to the next value, skipping one array position and leaving the default int value of 0 in the skipped position.

You need to fix the flow of control so that this doesn't happen. There are many different ways to fix these kinds of issues. You could change employeeIds to List<int> and then use a while loop to continue looping until you have the expected number of values.

Console.WriteLine("How many employee IDs would you like to register?");



string input = Console.ReadLine();

int length; 

if(int.TryParse(input, out length))
{
    List<int> employeeIds = new List<int>(); // ** changed **

    while (employeeIds.Count < length) // ** changed **
    {
        Console.Write("Please enter the employee ID.");
        string input2 = Console.ReadLine();
        int id;
        if(int.TryParse(input2, out id))
        {
            employeeIds.Add(id); // ** changed **
        }
        else
        {
            Console.WriteLine("Invalid input, please enter a valid integer number.");
        }
    }
    for (int i = 0; i < employeeIds.Count; i  ) // ** changed **
    {
        Console.WriteLine($"ID {i   1}:  \t{employeeIds[i]}");
    }
}
else
{
    Console.WriteLine("Input is invalid, please input an integer number.");
}

CodePudding user response:

Don't mind the formatting but, I made a few changes to the code. I added a line where it counts while the user is required to input the id number of the requested amount of Ids, I also defined a new variable string and assigned it to your invalid message to make it a lot easier.

        string invalid = "Invalid Input, please input a valid integer number!";
        Console.Write("How many employee IDs would you like to register? "); string input = Console.ReadLine(); int length;
        if (int.TryParse(input, out length)){List<int> employeeIds = new List<int>();
        while (employeeIds.Count < length){Console.Write($"Please enter the employee ID number {employeeIds.Count   1}:");string input2 = Console.ReadLine();int id;
                if (int.TryParse(input2, out id)){employeeIds.Add(id);}
                else{Console.WriteLine(invalid);}}
            for (int i = 0; i < employeeIds.Count; i  ){Console.WriteLine($"ID {i   1}:  \t{employeeIds[i]}");}}
        else{Console.WriteLine(invalid);}

Now the program will now print out "Please enter employee ID 1: " or until it reaches the desired amount. I also changed how the code is printed out in the console to make it look cleaner.

  • Related