Question: How do you fix this code snippet to make it print out the sum when the user enters a letter rather than a double? The

How do you fix this code snippet to make it print out the sum when the user enters a letter rather than a double? The user must enter at least one double when the program begins.
System.out.print("Enter a value: ");
double sum =0;
Scanner in = new Scanner(System.in);
boolean hasData = true;
do
{
double value = in.nextDouble();
sum = sum + value;
System.out.print("Enter a value to add, or a letter to quit: ");
}
while (in.hasNext());
System.out.println("sum "+ sum);
Question 7 options:
while (in.hasData());
while (!in.hasEnded());
while (in.hasNextDouble());
while (hasData);

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!