Hi this is a pat of my program but even do is not required I

Hi, this is a pat of my program but even do is not required I want to add a part where if the user enters a letter it gives them an error message and ask them to input a number, but is not working, It was working before, but I added another if so now is not working... Any help?? Thanks

public class randomWalk {
   public static Color color;
   public static Color lineColor;
   public static int steps = 0;

   //This is the main method gathers input from the user and then calls steps.
   public static void main(String[] args) {
       Scanner CONSOLE = new Scanner(System.in);
       int radius ;
       System.out.print(\"Please enter an integer for the radius of the circle (between 50 and 400): \");
       radius = CONSOLE.nextInt();
       while (!(radius >= 50 && radius <=400)) {
           if(radius < 50 || radius >400) {  
           System.out.println(\"Error: Radius entered is out of range\");
           System.out.print(\"Please enter the radius of the circle (between 50 and 400): letter \");
           if (CONSOLE.hasNextInt( )) {
               radius = CONSOLE.nextInt();
           }
         } //This is the part that is not working
           else {
           String trash = CONSOLE.next( );
           System.out.print(\"Error: Input is not a number, enter a number between 50 and 400:\");
           }
      }
  
       System.out.print(\"Please enter your circle color: (Red (r) or Blue (b)): \");
       CONSOLE.nextLine();
       String s = CONSOLE.nextLine();
     

Solution

Please follow the code and comments for description :

CODE :

public class randomWalk {
public static Color color;
public static Color lineColor;
public static int steps = 0;

//This is the main method gathers input from the user and then calls steps.
public static void main(String[] args) {
Scanner CONSOLE = new Scanner(System.in);
int radius ;
System.out.print(\"Please enter an integer for the radius of the circle (between 50 and 400): \");
radius = CONSOLE.nextInt();
while (!(radius >= 50 && radius <=400)) {
if(radius < 50 || radius >400) {
System.out.println(\"Error: Radius entered is out of range\");
System.out.print(\"Please enter the radius of the circle (between 50 and 400): letter \");
if (CONSOLE.hasNextInt( )) {
radius = CONSOLE.nextInt();
}
} //This is the part that is not working
else {
String trash = CONSOLE.next( );
System.out.print(\"Error: Input is not a number, enter a number between 50 and 400:\");   
}
}
  
System.out.print(\"Please enter your circle color: (Red (r) or Blue (b)): \");
CONSOLE.nextLine();
String s = CONSOLE.nextLine();
     

OUTPUT :

Please enter an integer for the radius of the circle (between 50 and 400): a
Exception in thread \"main\" java.util.InputMismatchException
   at java.util.Scanner.throwFor(Scanner.java:864)
   at java.util.Scanner.next(Scanner.java:1485)
   at java.util.Scanner.nextInt(Scanner.java:2117)
   at java.util.Scanner.nextInt(Scanner.java:2076)
   at Chegg.randomWalk.main(randomWalk.java:26)
Java Result: 1


Descritpion :

nextInt() can only return an int if the InputStream contains an int as the next readable token.

If you want to validate input, you should use something like nextLine() to read a full String, and use Integer.parseInt(thatString) to check if it is an integer.

The method will throw a

NumberFormatException - if the string does not contain a parsable integer.


Hope this is helpful.

Hi, this is a pat of my program but even do is not required I want to add a part where if the user enters a letter it gives them an error message and ask them t
Hi, this is a pat of my program but even do is not required I want to add a part where if the user enters a letter it gives them an error message and ask them t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site