Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Hi, I am working in a program, and everything is working but one little thing. T

ID: 3938735 • Letter: H

Question

Hi, I am working in a program, and everything is working but one little thing. This is part of my code but I want to be able to print something so the user knows he has to enter the number again... but if i remove the // it gives me an error about erorr: 'else' without 'if'... So in general the program runs fine I just want to make look nicer...

Hopefully you can help me... Many thanks

   public static void main(String[] args) {
       Scanner CONSOLE = new Scanner(System.in);
       int radius = 0;
       System.out.print("Please enter the radius of the circle (between 50 and 400): ");
       while (radius < 50 || radius > 400) {
         if (CONSOLE.hasNextInt( ))
           // System.out.print("Error: Radius entered is out of range");
           radius = CONSOLE.nextInt();
          // System.out.print("Please enter the radius of the circle (between 50 and 400): ");
         else {
           String trash = CONSOLE.next( );
           System.out.print("Error: Input is not a number, enter a number between 50 and 400:");
           }
       }
  

Explanation / Answer

Blocks were not correctly closed. Please find below the corrected code with comments removed for the lines to be printed to console and with proper indentation.

public static void main(String[] args) {
Scanner CONSOLE = new Scanner(System.in);
int radius = 0;
System.out.print("Please enter the radius of the circle (between 50 and 400): ");
while (radius < 50 || radius > 400) {
if (CONSOLE.hasNextInt( )) {
System.out.print("Error: Radius entered is out of range");
radius = CONSOLE.nextInt();
System.out.print("Please enter the radius of the circle (between 50 and 400): ");
}   
else {
String trash = CONSOLE.next( );
System.out.print("Error: Input is not a number, enter a number between 50 and 400:");   
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote