0

I updated my program. Here's what it does.
1. User inputs limit (must be a number)
2. If integer, stored in integer. Else, ask again. (already handled with if-else)
3. User inputs value (must be a number)
4. If integer, stored in integer array. Else, ask again. (this is the problem)

Console.Write("\n  Enter limit on how many number(s): ");
string limit = Console.ReadLine();

int value;
if (int.TryParse(limit, out value))
{
    int size = int.Parse(limit);
    int[] a = new int[size];

    for (int i = 0; i < size; i++)
    {
        //I need this part to filter first the input of user
        Console.Write("  Value: ");
        //If integer, I need it to be stored in the array
        a[i] = Convert.ToInt32(Console.ReadLine());

        /*string val = Console.ReadLine();
        int detect;
        if(!int.TryParse(Console.ReadLine(), out detect))
        {
            break;
        }
        else
        {

        }*/
    }
    int len = a.Length;
    Program pj = new Program();
    pj.programBExtension(a, len);
}
else
{
    Console.Clear();
    Console.WriteLine("\n  You have entered an invalid value. You will be asked again to enter the limit.");
    goto progB;
}
Shelby115
  • 2,816
  • 3
  • 36
  • 52
Lock
  • 1
  • 1
  • 4
  • Here http://stackoverflow.com/questions/24443827/reading-an-integer-from-user-Input are a lot of example. – naro Mar 17 '16 at 14:09
  • 2
    Also of note, TryParse sets the 'value' variable to the parsed value already so you don't need to parse it again, just use that variable – Gordon Allocman Mar 17 '16 at 14:10

2 Answers2

1

use the same tricky TryParse inside the Loop to;

for (int i = 0; i < size; i++)
{
    if(!int.TryParse(Console.ReadLine(),out a[i])
    {
        break; // This is wrong input handle this
    }
}
Shelby115
  • 2,816
  • 3
  • 36
  • 52
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

You are already filtering out strings which cannot be converted to integers so just use that value 'detect':

        string b = Console.ReadLine();
        int detect;
        if (int.TryParse(b, out detect)) <-- Will filter out strings here

            for (int i = 0; i < size; i++)
            {
                a[i] = detect;
            }
            int len = a.Length;
            Program pj = new Program();
            pj.programBExtension(a, len);
James Dev
  • 2,979
  • 1
  • 11
  • 16