Reputation: 81
In my program, I have the user input a radius value and then the program outputs the area and perimeter.
I want to make sure the user types in a number, so I used the hasNextDouble() method. It's not quite working correctly, though.
When the program runs the first while loop that I bolded (apparently, I can't bold code, so it's the code with asterisks surrounding it) in my code below, the words "Please enter a number > " appear as intended.
However, if the program runs the second while loop that I bolded (which is nested inside a while loop that tests if the number from the user is positive), "Please enter a number > " appears twice.
I can't figure out why those words are printing twice. Can anyone help?
/**
* Uses the Circle class to calculate area and perimeter of a circle based on a user-provided radius.
*
* @author Brittany Gefroh
* @version 1.0
*/
//Import the Scanner class
import java.util.Scanner;
public class CircleTest
{
public static void main (String [] args)
{
//Initialize a Scanner object
Scanner scan = new Scanner(System.in);
//Create new Circle object
Circle circle1 = new Circle();
//Declare variables
double input;
String garbage;
String answer;
//Do/while loop answer is Y or y
do
{
//Ask user for a radius value
System.out.print("Enter a radius value > ");
**while ( ! scan.hasNextDouble())
{
garbage = scan.nextLine();
System.out.print("\nPlease enter a number > ");
}**
//Assign user input to the input variable
input = scan.nextDouble();
//Test if input is a positive number
while (input <= 0)
{
//Prompt user for a new radius value
System.out.println("Radius must be greater than 0");
System.out.print("\nEnter a radius value > ");
**while ( ! scan.hasNextDouble())
{
garbage = scan.nextLine();
System.out.print("\nPlease enter a number > ");
}**
//Assign user input to the input variable
input = scan.nextDouble();
}
//Run the setRadius method to change the radius
circle1.setRadius(input);
//Print blank space
System.out.println("");
//Display output
System.out.println("The radius is " + circle1.getRadius());
System.out.println("The area is " + circle1.getArea());
System.out.println("The perimeter is " + circle1.getPerimeter());
//Print blank space
System.out.println("");
//Ask user if he/she wants to try again
System.out.print("Would you like to try again? Y or N > ");
answer = scan.next();
//Print blank space
System.out.println("");
}while (answer.equalsIgnoreCase("Y"));
}
}
Upvotes: 1
Views: 2431
Reputation: 2639
Change:
answer = scan.next();
To:
scan.nextLine();
answer = scan.nextLine();
Maybe you should try to simplify this code by creating a specialized method for reading double with validation attached to it? Also try to think why you have these 'empty' nextLine() operations. Are these necessary?
edit...
The problem is that scan.nextDouble();
do not remove EOL marker (End Of Line). The same is with scan.next();
. That is your issue. EOL marker is analized by while condition and it shows:
"Please enter a number > " <immediate EOL answer which was left in scanner>
"Please enter a number > " <now we are waiting for user input>
Upvotes: 2