1

Essentially, I'm trying to store user input in a multi dimensional array using for loops. If the user types in something which is not expected/wanted (e.g. less than 0), a warning message will be displayed and the loop should ideally 'wait' till it receives the next valid integer.

Currently, my code, as shown below, works OK but I'm wondering if there are any better/more optimized ways of doing this.

for (int row = 0; row < array.length; row++) {
    for (int column = 0; column < array[row].length; column++) {
        int number = input.nextInt();
        if((input.nextInt() >= 0) {
            array[row][column] = number;
        } else {
            System.out.println("Input must be > 0.");
            column--;
        }
}
user432584920684
  • 379
  • 1
  • 8
  • 19

4 Answers4

2

Use a do..while loop to wait until the user inputs something valid. This is cleaner than modifying a loop counter.

for (int row = 0; row < array.length; row++) {
        for (int column = 0; column < array[row].length; column++) {
            bool hasEnteredValidInput = false;
            do
            {
               int number = input.nextInt();
               if(number  >= 0) {
                  array[row][column] = number;
                  hasEnteredValidInput = true
               } else {
                  System.out.println("Input must be > 0.");
               }
            } while (!hasEnteredValidInput);
        }
    }

Even better would be to extract the reading code into it's own function:

for (int row = 0; row < array.length; row++) {
        for (int column = 0; column < array[row].length; column++) {
             array[row][column] = readValidInputFromUser();   
        }
 }

 public string readValidInputFromUser()
 {
    while(true)
    {
           int number = input.nextInt();
           if(number >= 0) {
              return number;
           } else {
              System.out.println("Input must be > 0.");
           }
    }
 }

This version makes it very clear what you're doing.

Oleksi
  • 12,947
  • 4
  • 56
  • 80
  • Thanks for this, tried it and worked great! - I have a quick question though. I assume I have to place `Scanner input = new Scanner(System.in)` in the function? Is that correct? Or is there another way to do this? – user432584920684 May 06 '12 at 04:12
  • Yeah, that's the correct place for the Scanner. You can also make it a global variable, but this is a worse solution. – Oleksi May 06 '12 at 04:14
  • Thanks for the help. It certainly looks a lot more 'modular' with a function added in! :) – user432584920684 May 06 '12 at 04:19
1

That's pretty much the way to do it. You can convert it to while loop, but I see no reason to do that.

int column = 0;
while ( column < array[row].length) {
        int number = input.nextInt();
        if((input.nextInt() >= 0) {
            array[row][column] = temp;
            ++column;
        } else {
            System.out.println("Input must be > 0.");
        }
}
MByD
  • 135,866
  • 28
  • 264
  • 277
1

You don't know how many times that the user is going to enter invalid input, so this is a prime example of when to use a while loop.

Try this:

for (int row = 0; row < array.length; row++) {
    for (int column = 0; column < array[row].length; column++) {
        int number = input.nextInt();
        if(number >= 0) {
            array[row][column] = temp;
        } 
        else 
        {
            while(number < 0)
            {
                System.out.println("Input must be > 0.");
                number = input.nextInt();
            } 
        }
}

Also notice in your if statement I changed if input.nextInt() to number because calling nextInt() again will read from whatever input stream you hooked to your scanner.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
0

decreament it in loop code on that perticular condition. And it seems you exactly did that in case of column--

Also you could do like this in your current for loop:

for (int row = 0; row < array.length; row++) {
    for (int column = 0; column < array[row].length; column++) {
        int number;
        do {
            number = input.nextInt();
            if(number<0)  System.out.println("Number should be >= 0, enter again");
        }while(number<0);
        array[row][column] = number;

    }
}
Nandkumar Tekale
  • 16,024
  • 8
  • 58
  • 85