• Register
1 vote
859 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,240 points

Related questions

0 votes
1 answer 5 views
5 views
How can I make the program ask for valid inputs instead of crashing when non-sensical data is entered?
asked 3 days ago TeamScript 11.1k points
1 vote
1 answer 26 views
26 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
1 vote
2 answers 709 views
709 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, 2020 Gavin 15.3k points
0 votes
1 answer 4 views
4 views
I&rsquo;m new. can anyone solve this problem?
asked Dec 14, 2020 TeamScript 11.1k points
1 vote
1 answer 27 views
27 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, 2020 adamSw 11.3k points
0 votes
2 answers 337 views
337 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 23 views
0 votes
1 answer 1.4K views
1.4K views
Problem : I am trying to pass the values from the servlet to jsp page But when I run the page I am facing below error: Error as below : java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed org.apache.catalina.connector. ... getRequestDispatcher("usertaskpage.jsp"); dd.forward(request, response); But still I got a same error. How can I fix above error?
asked Feb 3, 2020 jwilliam 3.9k points
0 votes
1 answer 5 views
5 views
Problem: The exercise is as follows: Given the height and base of a rectangle, find its area and perimeter. I started doing it the way they explained to me in class and this is my main code (I have the rectangle.h where I declare the class and rectangle.cpp where I define the methods). main.cpp ... .getY (); per = (2 * x) + (2 * y); cout << "The perimeter of the rectangle is" << per << endl; }
asked 9 hours ago sasha 5.9k points