I have a problem with my restaurant assignment - I'm required to
Create a while loop with a required quantity that user purchases to be between 1 and 15. User has 3 attempts and if after the third time he doesn't put the right quantity 1
Using a do- while loop user has to answer if he wants to run a program again. If he answers 'Y' (for yes) it runs again, if the answer is 'N' (for no ) it terminates.
I keep getting errors: the while loop does only 2 attempts, and the do while doesn't work at all.
import java.text.DecimalFormat;
import java.util.Scanner;
public class newone {
public static void main (String[] args){
char repeat;
// To hold 'y' or 'n'
String input;
DecimalFormat df = new DecimalFormat("$0.00");
//to use: df.format(doubleVariable)
//Display any welcome message at the top of the output screen
System.out.println("Welcome!");
double price = 5.0;
System.out.println("Burger is " + df.format(price));
Scanner keyboard = new Scanner(System.in);
int attempts = 0;
int maxAttempts = 3;
int quantity;
System.out.print("Enter amount of burgers: ");
quantity = keyboard.nextInt();
//LESS THAN OR EQUAL TO 3...
while(quantity <1 || quantity >=15 && attempts<=maxAttempts){
if(quantity < 1 || quantity >= 15){
System.out.println("That is an invalid amount, please try again");
quantity= keyboard.nextInt();
++attempts;
}
else if(quantity>1 && quantity<15){
//user entered a valid amount
System.out.println("The amount is valid. You ordered: "+ quantity);
double subTotal = quantity * price;
}
while (attempts>=maxAttempts){
System.out.println("Too many attempts, you can't order");
break;
}
}
do{
System.out.println("Would you like to run the program again?");
System.out.print("Enter Y for yes or N for no: ");
input = keyboard.nextLine(); // Read a line.
repeat = input.charAt(0); // Get the first char.
}
//it gives me an error Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
at java.lang.String.charAt(Unknown Source)
while (repeat == 'Y' || repeat == 'y');
}
}}