Home > Net >  Repeating a statement in while loop Java
Repeating a statement in while loop Java

Time:09-10

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.

CodePudding user response:

I think the problem is located in your

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.

CodePudding user response:

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.

CodePudding user response:

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

                    
            }
  •  Tags:  
  • java
  • Related