How do I force the user to enter the correct value after they have already entered the wrong value (out of range)?

System.out.println("Enter the first test score:");

double test1 = input.nextInt();

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");

System.out.println("Enter the second test score:");

double test2 = input.nextInt();

if (!(test2 >= 0  && test2 <= 100))
System.out.println("This is out of the acceptable range, please enter a number between 0 and 100 .");

      

If I ask the user to enter a value for test1 and its or in a certain range, how can I again prompt him to enter the correct value ?. now if I run and enter the wrong value "This is out of valid range, enter a number between 0 and 100" msg, it will appear, but it will be right on test2. i tried to do it

System.out.println("Enter the first test score:");

double test1 = input.nextInt();

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
double test1 = input.nextInt();

      

but I am getting an error. Do I need to use a loop, and if so, how?

+3


source to share


3 answers


This is because you are declaring twice test1

.

double test1 = input.nextInt();

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
-->  double test1 = input.nextInt(); //Look at this line

      

Just try this: double test1 = input.nextInt ();

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
test1 = input.nextInt(); 

      



For cleaner code, use the following method:

public double getValue(){
        double test1=input.nextInt();
        if (!(test1 >= 0  && test1 <= 100)){
            return getValue();
        }else{
            return test1;
        }
    }

      

The above code will continue to enter custom inputs if the input does not meet the conditions !(test1 >= 0 && test1 <= 100)

.

+1


source


First you declare test1 twice, second you can use loop



    double test1 = input.nextInt();
    while(!(test1 >= 0 && test1 <= 100))
    {
        System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
        /*double */test1 = input.nextInt();
    }

      

0


source


try something like

double test1 = input.nextInt();
while(!(test1 >= 0 && test1 <= 100)) {
     System.out.println("Enter a valid number"); 
     test1.nextInt();
}

      

The idea is to prompt the user until they enter valid input. If you use simple if statements or fixed for-loop, you will only get a request for a fixed amount for the correct value.

One more thing you should keep in mind:

if (!(test1 >= 0  && test1 <= 100))
System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
double test1 = input.nextInt(); //There is no need to declare test1 again. Remove double

      

will compile as

if (!(test1 >= 0  && test1 <= 100))
     System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");

test1 = input.nextInt(); //This is not part of the if!!!!

      

so you need to use parentheses like

if (!(test1 >= 0  && test1 <= 100)) {
    System.out.println("This is out of the acceptable range, please enter a  number between 0 and 100 .");
    test1 = input.nextInt(); 
}

      

0


source







All Articles