How do I loop the following program? I need it to loop when the input is invalid
ID: 3567891 • Letter: H
Question
How do I loop the following program? I need it to loop when the input is invalid. I used a switch operator but now need it to loop. Please help.
public class Project
{
public static void main(String[] args)
{
//Variables
String input;
char option;
int numMeals;
int noMore = 0;
double spaghettiPrice = 14.99;
double lasagnaPrice = 12.99;
double fettucciniPrice = 9.99;
double pizzaPrice = 9.99;
//Scanner object
Scanner keyboard = new Scanner(System.in);
do
{
//Display Dinner Menu
for(int i = 0; i <= 44; i++)
{
System.out.print("=");
}//End for loop
System.out.println();
System.out.println("| DINNER MENU |");
for(int i = 0; i <= 44; i++)
{
System.out.print("=");
}//End for loop
System.out.println();
System.out.println("| OPTION | DESCRIPTION | COST |");
for(int i = 0; i <= 44; i++)
{
System.out.print("=");
}//End for loop
//Add line
System.out.println();
System.out.println("| A | Spaghetti and Meatballs | $" + spaghettiPrice + " |");
System.out.println("| B | Lasagna | $" + lasagnaPrice + " |");
System.out.println("| C | Fettuccini Alfredo | $ " + fettucciniPrice + " |");
System.out.println("| D | Pizza | $ " + pizzaPrice + " | ");
System.out.println("| E | Display Tip Table | | ");
System.out.println("| F | Check Out | | ");
System.out.println("| G | Exit | | ");
for(int i = 0; i <= 44; i++)
{
System.out.print("=");
}//End for loop
//Add line
System.out.println();
//Display Dinner Menu
//Ask user to select an option
System.out.print(" Please select an option from the menu: ");
input = keyboard.nextLine();
option = input.charAt(0);
//Add line
System.out.println();
//Display selection
switch(option)
{
case 'A':
case 'a':
System.out.println("Spagetti and Meatballs selected");
noMore = 1;
break;
case 'B':
case 'b':
System.out.println("Lasagna selected");
noMore = 1;
break;
case 'C':
case 'c':
System.out.println("Fettuccini Alfredo selected");;
break;
case 'D':
case 'd':
System.out.println("Pizza selected");
break;
case 'E':
case 'e':
System.out.println("Display Tip Table selected");
break;
case 'F':
case 'f':
System.out.println("Check Out selected");;
break;
case 'G':
case 'g':
System.out.println("Exit selected");
break;
default:
System.out.println("Invalid option");
noMore = 0;
}
}
while (noMore == 1);
Explanation / Answer
//Java program repeats the menu until user selects a valid choice.
import java.util.Scanner;
public class Project {
public static void main(String[] args)
{
// Variables
String input;
char option;
int numMeals;
int noMore = 0;
double spaghettiPrice = 14.99;
double lasagnaPrice = 12.99;
double fettucciniPrice = 9.99;
double pizzaPrice = 9.99;
//create a boolean variable
//set the boolean variable to false.
boolean repeatLoop=false;
// Scanner object
Scanner keyboard = new Scanner(System.in);
do
{
// Display Dinner Menu
for (int i = 0; i <= 44; i++)
{
System.out.print("=");
}// End for loop
System.out.println();
System.out.println("| DINNER MENU |");
for (int i = 0; i <= 44; i++)
System.out.print("=");
System.out.println();
System.out.println("| OPTION | DESCRIPTION | COST |");
for (int i = 0; i <= 44; i++)
System.out.print("=");
// Add line
System.out.println();
System.out.println("| A | Spaghetti and Meatballs | $" +
spaghettiPrice + " |");
System.out.println("| B | Lasagna | $" + lasagnaPrice + " |");
System.out.println("| C | Fettuccini Alfredo | $ "
+ fettucciniPrice +
" |");
System.out.println("| D | Pizza | $ " + pizzaPrice + " | ");
System.out.println("| E | Display Tip Table | | ");
System.out.println("| F | Check Out | | ");
System.out.println("| G | Exit | | ");
for (int i = 0; i <= 44; i++)
System.out.print("=");
// Add line
System.out.println();
// Display Dinner Menu
// Ask user to select an option
System.out.print("Please select an option from the menu: ");
input = keyboard.nextLine();
option = input.charAt(0);
// Add line
System.out.println();
// Display selection
switch (option)
{
case 'A':
case 'a':
System.out.println("Spagetti and Meatballs selected");
noMore = 1;
//set to false if choice is valid
repeatLoop=false;
break;
case 'B':
case 'b':
System.out.println("Lasagna selected");
noMore = 1;
//set to false if choice is valid
repeatLoop=false;
break;
case 'C':
case 'c':
System.out.println("Fettuccini Alfredo selected");;
//set to false if choice is valid
repeatLoop=false;
break;
case 'D':
case 'd':
System.out.println("Pizza selected");
//set to false if choice is valid
repeatLoop=false;
break;
case 'E':
case 'e':
System.out.println("Display Tip Table selected");
//set to false if choice is valid
repeatLoop=false;
break;
case 'F':
case 'f':
System.out.println("Check Out selected");
//set to false if choice is valid
repeatLoop=false;
break;
case 'G':
case 'g':
System.out.println("Exit selected");
//set to false if choice is valid
repeatLoop=false;
break;
default:
System.out.println("Invalid option");
System.out.println("Please!. Make a valid menu choice");
noMore = 0;
//set to true if choice is invalid so that while loop condition
//becomes true and loop repeats for menu choice.
repeatLoop=true;
}
}while (repeatLoop);
}
}
------------------------------------------------------------------------------
Sample output:
=============================================
| DINNER MENU |
=============================================
| OPTION | DESCRIPTION | COST |
=============================================
| A | Spaghetti and Meatballs | $14.99 |
| B | Lasagna | $12.99 |
| C | Fettuccini Alfredo | $ 9.99 |
| D | Pizza | $ 9.99 |
| E | Display Tip Table | |
| F | Check Out | |
| G | Exit | |
=============================================
Please select an option from the menu: o
Invalid option
Please!. Make a valid menu choice
=============================================
| DINNER MENU |
=============================================
| OPTION | DESCRIPTION | COST |
=============================================
| A | Spaghetti and Meatballs | $14.99 |
| B | Lasagna | $12.99 |
| C | Fettuccini Alfredo | $ 9.99 |
| D | Pizza | $ 9.99 |
| E | Display Tip Table | |
| F | Check Out | |
| G | Exit | |
=============================================
Please select an option from the menu: -
Invalid option
Please!. Make a valid menu choice
=============================================
| DINNER MENU |
=============================================
| OPTION | DESCRIPTION | COST |
=============================================
| A | Spaghetti and Meatballs | $14.99 |
| B | Lasagna | $12.99 |
| C | Fettuccini Alfredo | $ 9.99 |
| D | Pizza | $ 9.99 |
| E | Display Tip Table | |
| F | Check Out | |
| G | Exit | |
=============================================
Please select an option from the menu: j
Invalid option
Please!. Make a valid menu choice
=============================================
| DINNER MENU |
=============================================
| OPTION | DESCRIPTION | COST |
=============================================
| A | Spaghetti and Meatballs | $14.99 |
| B | Lasagna | $12.99 |
| C | Fettuccini Alfredo | $ 9.99 |
| D | Pizza | $ 9.99 |
| E | Display Tip Table | |
| F | Check Out | |
| G | Exit | |
=============================================
Please select an option from the menu: g
Exit selected
Hope this would be helpful .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.