• Register
1 vote
408 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

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,240 points

Related questions

1 vote
1 answer 20 views
20 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 Gavin 15.3k points
1 vote
2 answers 286 views
286 views
Problem: Well, I need to write a program that will re-prompt the user after invalid input in java. Let&rsquo;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 ... ); System.out.println(""); System.out.println("Put the value again, please\n"); value = userInput.nextInt(); } Many thanks.
asked Apr 6 Gavin 15.3k points
1 vote
1 answer 25 views
25 views
Problem: I am a new Java developer. You may call me a student as I am still learning it. I am trying to make a simple Java program. In my program, I tried to taking string input from the user and then store the value in a variable, and afterward, I ... NumberFormatException: For input string: "Name" What is the reason that causes the error? Could any of you please shed some light on this? Thanks.
asked May 30 adamSw 11.3k points
0 votes
2 answers 247 views
247 views
Problem : I am getting bellow error when I try to use Integer.parseInt() with a single char exception in thread "main" java.lang.numberformatexception: for input string: "s"
asked Oct 19, 2019 peterlaw 6.9k points
0 votes
1 answer 16 views