Repeating a statement in while loop Java

Viewed 73

Sorry for the newbish question, am quite new with Java.

So I want to display an error message when user input is outside of the bounds (Lesser than 0, greater than 100) which I've managed to do but I also want that the user can try again but my current code only continues with the execution of the program.

This is what I have now:

import java.util.Scanner;

public class storeQuota {
   
        public static void main(String [] args) {
        
            Scanner input = new Scanner (System.in);

        
            int quotas [] = new int [100];
            int NumberOfWorkers = 100;

            

           
            for (int i = 0; i<numberOfWorkers; i++) {

                if (i == 0) {
                    System.out.print("Enter the quota for the 1st student: ");
                } 
                else if (i == 1) {
                    System.out.print("Enter the quota for the 2nd student: ");
                } 
                else if (i == 2) {
                    System.out.print("Enter the quota for the 3rd student: ");
                } 
                else if (i >= 3) {
                    System.out.print("Enter the quota for the " + (i+1) + "th student: ");
                }
                     
                while (true) {
                    quotas[i] = input.nextInt();
                        
                    if (quotas[i] > 100 || quotas[i] < 0) 
 
                        System.out.println("Error - Can only be between 0 and 100.");
                        break;
                        
                }
                
            }          
                
            

          //Printing all quotas. 
            System.out.println("Thank you for your input. Your entered quotas are: ");

            for (int i=0; i<numberOfWorkers; i++)   
                    {  
                        System.out.print(quotas[i] + ", ");  
                    }  

            
            
     input.close();
            
        
        }
        
}


With this code, the error message is correctly displayed when a user inputs an int that isn't between 0 and 100 but the user will be unable to try again, the program continues to ask for the next quoata.

4 Answers

I think the problem is located in this line

break;

after

System.out.println("Error - Can only be between 0 and 100.");

which always breaks the while loop. Instead you only want to break the while loop if the input is in valid range. I would not use while(true) but some sort of conditional variable which is set to false in the while loop if the input is in valid range, also because while(true) is not a good programming practice from my point of view.

Your problem is using Break; rather than using that, you should change the while(true) to while(false), you've also forgot to add curly brackets around the if statement.

boolean x = true;
while (x){
quotas[i] = input.nextInt();
if (quotas[i] > 100 || quotas[i] < 0){ 
    System.out.println("Error - Can only be between 0 and 100.");
    x = false;
   }
}

also I suggest learning exceptions as they would make this 10x easier.

When executed, "break" breaks the loop you are currently in. In your code, break is executed irrespective of what the input is resulting in the unwanted result.

Simplest solution would be (closest to your original code):

while(true) {
    quotas[i] = input.nextInt();
    if (quotas[i] > 100 || quotas[i] < 0) {
        System.out.println("Error - Can only be between 0 and 100.");
    } else {
        break;
    }
}

Here, the loop will break only if correct input is entered.

You haven't used curly braces in if condition.

while (true) {
 quotas[i] = input.nextInt();
                        
 if (quotas[i] > 100 || quotas[i] < 0) {
 
 System.out.println("Error - Can only be between 0 and 100.");
  break;
  }
}
Related