• Register
1 vote
1.4k views

Problem:

So, guys, I am learning to handle the java scanner class and taking input from the user. Let’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 to make it inside my head.

  1. Taking input by using scanner class
  2. Pass it through a true-false condition
  3. Print something according to the true-false 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.

12 7 7
15,250 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution:

Well, your flow-chart looks good. It’s always better to give it a try to write code. Let’s write a code using the scanner class by following your given flowchart.

import java.util.Scanner;

public class Prompting{

     public static void main(String []args){
         Scanner in = new Scanner (System.in);
         boolean valid = false;
         double input = 0;
         while(!valid){
             System.out.println("Enter a positive value<100 : ");
             input = in.nextDouble();
             if (input>0 && input<100){
                 valid = true;
                 System.out.println("This is a good number");
             }
             else{
                 System.out.println("Sorry! Invalid Input");
             }
         }
     }
}

As per your flowchart, I made this program. Here, this program will take input and check it if it matches our requirement. It counts only 0 to 100 positive numbers except all it throws back Sorry Invalid Input.

So, run the program and let me know if you have got any questions in the future regarding this issue. Please reply to this thread.
Thanks.

13 9 6
94,260 points

Related questions

0 votes
1 answer 28 views
28 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
1 vote
1 answer 40 views
40 views
Problem: Hello, I was trying to compile my java code but it throws me an error below: invalid escape sequence (valid ones are \b \t \n \f \r \ \' \\ )" It happened when I was trying to taking input by a Scanner class in my code. I am really ... here? I must appreciate your help. Here is the code snippet: InputReader job = new InputReader (new InputReader("\My Documents\job\jobfile.txt")); Thanks.
asked Mar 20, 2020 Gavin 15.3k points
0 votes
1 answer 3 views
3 views
Problem: I am stuck in this program that is string method, my issue is that I cannot get the loop to stop and the program to print the output that is currently stored after the keyword has been entered. I am not trying to compare strings, I am trying to input multiple strings and add a word, in ... nextLine(); s = s + "not "; if(s == "stop"){ System.out.println(s); break; } else{ continue; } } } }
asked May 1 ochena 23.3k points
0 votes
1 answer 6 views
6 views
Problem: anyone who know about this problem please help? discord how to give someone a role when they join
asked Mar 30 Rohit kr 17.5k points
0 votes
1 answer 18 views
18 views
Problem how to make chrome not load tabs until they are selected
asked Feb 25 jyoti goyal 2.1k 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 4 views
4 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 14 views
14 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 5 views
5 views
Problem: Hi, any idea that what should I do to fix it&hellip;I am new in php
asked Mar 12 Ifra 37.2k points
0 votes
1 answer 52 views
52 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