• Register
1 vote
1.3k 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,260 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 49 views
49 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
1 answer 3 views
3 views
Problem: How do I write a program that uses raw_input to prompt a user for their name and then welcomes him.
asked Apr 25 zayed1 58.7k points
0 votes
1 answer 4 views
4 views
I need arrange a program that uses input to prompt , how can i write it ?
asked Apr 22 Sifat55 280 points
0 votes
1 answer 4 views
4 views
Problem How can I read input from the console using the Scanner class in Java? Please tell me the correct choice for the following question:
asked Apr 9 neeraj 10.5k points
0 votes
1 answer 3 views
3 views
Problem: who know this problem please help? how to add user input to an array in java
asked Apr 3 Rohit kr 17.5k points
0 votes
1 answer 8 views
8 views
Problem: anyone who know this problem please help? how to store user input into an arraylist in java
asked Mar 30 Rohit kr 17.5k points
0 votes
1 answer 6 views
6 views
Problem: I need someone&rsquo;s assistance to solve my problem .. prompt the user to enter a string of their choosing. store the text in a string. output the string.
asked Apr 7 Ifra 34.7k points
0 votes
1 answer 8 views
8 views
Problem: I am somewhat lost on what to do. There are 4 parts. Prompt the user for a string that contains two strings separated by a comma. Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the ... word: " + firstWord); System.out.println("Second word:" + secondWord); System.out.println(); System.out.println(); } } return;
asked 5 days ago ochena 22.1k points
0 votes
1 answer 3 views
3 views
Problem: I am somewhat lost on what to do. There are 4 parts. Prompt the user for a string that contains two strings separated by a comma. Report an error if the input string does not contain a comma. Continue to prompt until a valid string is entered. Note: If the input ... " + firstWord); System.out.println("Second word:" + secondWord); System.out.println(); System.out.println(); } } return; } }
asked 5 days ago muktaa 34.4k points