user432584920684
user432584920684

Reputation: 389

Stopping increment in a for loop for a specific case in Java

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--;
        }
}

Upvotes: 1

Views: 3503

Answers (4)

Nandkumar Tekale
Nandkumar Tekale

Reputation: 16158

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;

    }
}

Upvotes: 0

Oleksi
Oleksi

Reputation: 13097

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.

Upvotes: 2

Hunter McMillen
Hunter McMillen

Reputation: 61520

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.

Upvotes: 1

MByD
MByD

Reputation: 137382

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.");
        }
}

Upvotes: 1

Related Questions