Hi I am working in a program and everything is working but o
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:\");
}
}
Solution
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:\");
}
}
}
