Question: The following do - while loop is suppose to ask the price for a gallon of gas. The price must a positive number. The price

The following do-while loop is suppose to ask the price for a gallon of gas. The price must a positive number. The price can be an integer value or a double value. for example the price can be 3 or 3.0 or 3.5.
To create a robust program, we must do the data validation and as long as the user is entering a negative value or a String, the program must keep asking the user to enter a valid input.
in this program you need to use some of the scanner methods listed here, make sure to use the proper ones: hasNextInt(), hasNextDouble(), hasNextLine(), hasNext(), nextInt(), nextDouble(), nextLine()
Must flush the buffer at the proper locations in the code
sample output:
Enter the price for a gallon of gas: dfdfdf
Enter the price for a gallon of gas: sadfdf
Enter the price for a gallon of gas: -45
Enter the price for a gallon of gas: -3
Enter the price for a gallon of gas: 5.5
The price 5.5 you entered is a valid amount
Fill in the blank so that the do-while loop works based on the given description:
Must use a do-while and some conditional statements
double price =0;
{
System.out.print("Enter the price for a gallon of gas: ");
if(!kb.hasNextDouble() kb.)
kb. ; // flush the buffer
else
{
price = kb. ;
kb. ; // flush the buffer
}
}(<=0);
System.out.println("The price "+ price +" you entered is a valid amount");

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!