• Register
1 vote
899 views

Problem:

Well, I need to write a program that will re-prompt the user after invalid input in java. Let’s say my program prompt value from the user and store in a variable. Then it will compare the variable and my desire value. If the value satisfies my condition it should print out directly, and if not it will ask for a re-prompt.
I hope I explained my problem clearly. Now, I am looking for a solution from you guys. Please, find my sample code below and check where I am doing wrong

System.out.println("What is your expected value?\n");
    value = userInput.nextInt();
    if((value > 100) || (value < 1)) {
        System.out.println("ERROR Please enter a valid value");
        System.out.println("");
        System.out.println("Put the value again, please\n");
        value = userInput.nextInt();
    }

Many thanks.

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote

Solution:

You need to remember that, the if-statement is a boolean type operator. In your case, you had to use a while-loop for getting the re-prompt option. Let’s replace the IF with a while-loop. 

while (true) {
    System.out.println("What is your expected value?\n");
    value = userInput.nextInt();
    if ((value > 100) || (value < 1))
        System.out.println("ERROR! Please enter a valid value\n");
    else
        break;
}

This while break keeps checking the user prompted value and compares with the condition. Once it satisfies the condition it breaks the loop.

Good Day!

13 9 6
94,240 points
0 votes

You have to know that the if is the Boolean type operator.

Solutions:

 You have to use another loop like while, do-while loop to solve your problem.

Replace if with the while:

System.out.println("What is your age?\n");

age = userInput.nextInt();

while((age > 120) || (age < 1)) {//error message

    System.out.println("ERROR Please enter a valid age");

    System.out.println("");

    System.out.println("What is your age?\n");

    age = userInput.nextInt();

}//end i

If the user enters an invalid character like “E” or anything else which is not a number, then the loop will exit and if the age is less than 1 year or greater than 120 years then it will ask the user again to enter the correct values. The loop will continue to looping until the conditions are met.

Use the do-while loop:

boolean valid;

do {

     System.out.println("What is your age?\n");

     age = userInput.nextInt();

     valid = age > 1 && age < 120;

     if (!valid) {

       System.out.println("ERROR Please enter a valid age");

     }

}while (!valid);

 

To read the user input, it will prompt the user to type her input into the console and will continue to looping until the valid values found.

I hope this will help to solve your problem.

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 17 views
17 views
if the user entered an invalid input after they were prompted again, the program would just continue, how can I overcome this?
asked Feb 4 TeamScript 13.5k points
0 votes
2 answers 220 views
0 votes
1 answer 40 views
40 views
I was working on that. I just want to know how to do that?
asked Nov 5, 2020 TeamScript 13.5k points
1 vote
1 answer 1K views
1K views
Problem: So, guys, I am learning to handle the java scanner class and taking input from the user. Let&rsquo;s come to my point quickly. I was thinking to write a java program that asking the user for input until they give a valid response. I do have an idea ... condition So, this is my raw flow-chart to write the program. Please correct me if I am wrong anywhere. Any sample program is appreciated.
asked Mar 23, 2020 Gavin 15.3k points
0 votes
1 answer 14 views
14 views
How can I make the program ask for valid inputs instead of crashing when non-sensical data is entered?
asked Jan 22 TeamScript 13.5k points
0 votes
1 answer 16 views
16 views
How can I do this?
asked Jan 19 TeamScript 13.5k points
0 votes
1 answer 10 views
10 views
Problem: hi Loops are make to easy programming while loop,do-while loop and for loop easy concepts but nested loop is difficult to understand. it have many difficulties because it makes mix of loops.anyone can help to understand the concept of nested loop in java.
asked Nov 2, 2020 Mashhoodch 10.2k points
0 votes
1 answer 59 views
59 views
We are trying to find a method to take a char input in Java applying scanner class.
asked Sep 26, 2020 Daniel Anderson 4k points
0 votes
1 answer 11 views
11 views
can someone help me about the bufferredReader in java and how to input a character?
asked Sep 14, 2020 Daniel Anderson 4k points