So I am trying to write a program to order items.
I must use switch statements to do this but the problem I am having is if I use the switch statement if I try to use a second switch after it also gets called during the first
And even asking for other input after the switch statement should be finished it seems to not call them correctly.
The code I tried is this
public static void main(String[] args) {
int menu = 0;
int seldelivery = 0;
double delivery = 0;
double selection = 0;
String name;
String student;
String mobile;
Scanner reader = new Scanner (System.in);
System.out.println("Please select\n1:Coffee\n2:Tea\n3:Quit");
menu = reader.nextInt();
switch (menu){
case 1:
System.out.println("1 Cappucino €2.00\n2 Latte €2.00\n3 Espresso €1.50\n4 Americano €1.70");
menu = reader.nextInt();
if (menu == 1) {
selection = 2.00;
System.out.println("You have selected Cappucino");
} else if (menu == 2) {
selection = 2.00;
System.out.println("You have selected Latte");
} else if (menu == 3) {
selection = 1.50;
System.out.println("You have selected Espresso");
} else if (menu == 4) {
selection = 1.70;
System.out.println("You have selected Americano");
} else {
System.out.println("inavlid selection please try again");
}
break;
case 2:
System.out.println("1.Bewley’s Breakfast Tea (Pot for €1.80)\n2.Peppermint €1.50\n3 Camomile €1.50");
menu = reader.nextInt();
if (menu == 1) {
selection = 1.80;
System.out.println("You have selected Bewley’s Breakfast Tea");
} else if (menu == 2) {
selection = 1.50;
System.out.println("You have selected Peppermint");
} else if (menu == 3) {
selection = 1.50;
System.out.println("Camomile");
} else {
System.out.println("Invalid selection");
}
break;
case 3: //exit
System.exit(0);
default:
System.out.println("Invalid selection");
break;
}
System.out.println("Please choose one of the following");
System.out.println("1 Delivery €1.00 extra");
System.out.println("2 Pickup at canteen");
delivery = reader.nextInt();
switch (seldelivery) {
case 4:
System.out.println("1 Delivery will add €1.00");
break;
case 5:
System.out.println("2 Please head to the canteen to collect your order");
break;
default:
System.out.println("Invalid selection");
break;
}
System.out.println("Please enter your full name including your middle name");
name = reader.nextLine();
System.out.println("Please enter your student number");
student = reader.nextLine();
System.out.println("Please enter your phone number");
mobile = reader.nextLine();
}
}
I tried writing the code like this instead:
public static void main(String[] args) {
int menu = 0;
int menu1;
double delivery = 0;
double selection = 0;
String name;
String student;
String mobile;
Scanner reader = new Scanner(System.in);
System.out.println("Please select\n1:Coffee\n2:Tea\n3:Quit");
menu = reader.nextInt();
switch (menu) {
case 1:
System.out.println("1 Cappucino €2.00\n2 Latte €2.00\n3 Espresso €1.50\n4 Americano €1.70");
menu = reader.nextInt();
if (menu == 1) {
selection = 2.00;
System.out.println("You have selected Cappucino");
} else if (menu == 2) {
selection = 2.00;
System.out.println("You have selected Latte");
} else if (menu == 3) {
selection = 1.50;
System.out.println("You have selected Espresso");
} else if (menu == 4) {
selection = 1.70;
System.out.println("You have selected Americano");
} else {
System.out.println("inavlid selection please try again");
menu = reader.nextInt();
}
System.out.println("Please choose one of the following\n1 Delivery €1.00 extra\n2 Pickup at canteen");
menu1 = reader.nextInt();
switch (menu1) {
case 1:
System.out.println("1 Delivery will add €1.00");
if (menu1 == 1) {
delivery = 1.00;
System.out.println("You have selected to have your order delivered");
}
break;
case 2:
System.out.println("2 Please head to the canteen to collect your order");
if (menu1 == 2) {
delivery = 0.00;
System.out.println("Please head to the canteen to collect your order");
}
break;
default:
System.out.println("Invalid selection");
break;
}
break;
case 2:
System.out.println("1.Bewley’s Breakfast Tea (Pot for €1.80)\n2.Peppermint €1.50\n3 Camomile €1.50");
menu = reader.nextInt();
if (menu == 1) {
selection = 1.80;
System.out.println("You have selected Bewley’s Breakfast Tea");
} else if (menu == 2) {
selection = 1.50;
System.out.println("You have selected Peppermint");
} else if (menu == 3) {
System.out.println("Camomile");
}
break;
case 3: //exit
System.exit(0);
default:
System.out.println("Invalid selection");
break;
}
}
This stops the whole problem with the switches calling when they should not but again if I try to get the strings I need it would print them again to quickly.
The only way I can see doing this is to ask for all the strings needed within the switch and doing all validation as well.
Is this the correct way of doing it or am I missing something here ?
Any help is appreciated thanks :)
You misunderstand how java syntax is working.
In your second example, you switch on menu:
switch (menu) {
case 1:
//if you have reached this, then menu is 1 and there is no further need to if on it, because it is 1, nothing else. It is exactly one.
}
Related
This is the program, I designed it to be a phone store, and the problem I am facing is that after you choose to go back, the program will go back normally, but after you choose to buy the next time it restarts the program. What I want is to make it stop at buy directly.
import java.util.Scanner;
public class MyStore {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
Phones phOne = new Phones("iPhone 13.");
phOne.phs(256);
phOne.php(904);
Phones phTwo = new Phones("Galaxy s22.");
phTwo.phs(512);
phTwo.php(1199);
Phones phThree = new Phones("Huawei p50 pocket.");
phThree.phs(512);
phThree.php(1699);
System.out.println("Welcom To My Store!\n-------*****-------");
int i = 1;
do {
System.out.println("Choose a phone please:");
System.out.println("1-iPhone 13.\n2-Galaxy s22.\n3-Huawei p50 pocket.");
int a = scan.nextInt();
System.out.println("-------*****-------");
switch(a) {
case 1:
phOne.print();
System.out.println("1-buy.\n2-go back.");
int bg1 = scan.nextInt();
if(bg1 == 1) {
System.out.println("Congratulations! we will deliver it to you.");
--i;
}
else if(bg1 == 2){
++i;
}
else {
System.out.println("Please choose 1 or 2.");
}
break;
case 2:
phTwo.print();
System.out.println("1-buy.\n2-go back.");
int bg2 = scan.nextInt();
if(bg2 == 1) {
System.out.println("Congratulations! we will deliver it to you.");
--i;
}
else if(bg2 == 2){
++i;
}
else {
System.out.println("Please choose 1 or 2.");
}
break;
case 3:
phThree.print();
System.out.println("1-buy.\n2-go back.");
int bg3 = scan.nextInt();
if(bg3 == 1) {
System.out.println("Congratulations! we will deliver it to you.");
i-=2;
}
else if(bg3 == 2){
++i;
}
else {
System.out.println("Please choose 1 or 2.");
}
break;
default:
System.out.println("Please choose 1, 2, or 3.");
}
}while(i >= 1);
}
}
And this is the rest of the program if you need it.
import java.text.NumberFormat;
public class Phones {
NumberFormat nf = NumberFormat.getCurrencyInstance();
String name;
int storage;
double price;
public Phones(String name) {
this.name = name;
}
public void phs(int phs) {
storage = phs;
}
public void php(double php) {
price = php;
}
public void print() {
System.out.println("Model: "+name);
System.out.println("Storage: "+storage+"G");
System.out.println("Price: "+nf.format(price));
}
}
If you want the program to stop after buying a phone you'd want to implement the do-while loop with a boolean value, then after the user buys a phone set the boolean value to false.
boolean loop = true;
do {
System.out.println("Choose a phone please:");
System.out.println("1-iPhone 13.\n2-Galaxy s22.\n3-Huawei p50 pocket.");
int a = scan.nextInt();
System.out.println("-------*****-------");
switch(a) {
case 1:
phOne.print();
System.out.println("1-buy.\n2-go back.");
int bg1 = scan.nextInt();
if(bg1 == 1) {
System.out.println("Congratulations! we will deliver it to you.");
loop = false;
}
......
while(loop);
I'd recommend taking better care of your variable names. Make them obvious and meaningful, makes it easier to debug in the end!
I cannot figure out how to have the program loop through the Conversion Menu and then return to the Main Menu from choice "4 Return" as shown in the example.
Example of what the program output might look like:
Main Menu
Enter Distance
Quit the program
Please enter your choice: 1
Enter a distance in meters: 500
Conversion Menu
Convert to kilometers
Convert to inches
Convert to feet
Return
Enter your choice: 1
500 meters is 0.5 kilometers
Conversion Menu
Convert to kilometers
Convert to inches
Convert to feet
Return
Enter your choice: 3
500 meters is 1640.5 feet
Conversion Menu
Convert to kilometers
Convert to inches
Convert to feet
Return
Enter your choice: 4
Main Menu
Enter Distance
Quit the program
Please enter your choice: 2
Good Bye!
I've found multiple solutions for the program when it doesn't include the main menu and the looping back to it but I can't seem to find anything on this.
Here's what I have:
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int choice;
int option;
double meters = 0;
conversionControl();
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("\nEnter a Distance in Meters:");
meters = keyboard.nextDouble();
break;
case 2:
quitProgram();
break;
default:
showError("Please Enter a Valid Option");
conversionControl();
option = keyboard.nextInt();
if (option == 1) {
System.out.println("\nEnter a Distance in Meters:");
meters = keyboard.nextDouble();
}
else if ( option == 2) {
quitProgram();
}
break;
}
do{
menu();
choice = keyboard.nextInt();
switch (choice) {
case 1:
showKilometers(meters);
break;
case 2:
showInches(meters);
break;
case 3:
showFeet(meters);
break;
case 4:
conversionControl();
option = keyboard.nextInt();
if (option == 1) {
System.out.println("\nEnter a Distance in Meters:");
meters = keyboard.nextDouble();
}
else if ( option == 2) {
quitProgram();
}
break;
default:
showError("Please Enter a Valid Option");
menu();
choice = keyboard.nextInt();
break;
}
} while(choice != 0); {
}
}
I guess I did figure my own way out but I keep thinking it isn't the correct way or there's an easier way. Plus, some errors occur when testing some inputs (mainly the the showError method calls will output either the incorrect menu or it will just close the program after so many wrong inputs).
Any help/constructive criticism would be greatly appreciated. I'm somewhat new to coding (know HTML) and new to to this site as well.
Thank you!
Bob
As it look like an exercise I won't give you a complete code, but a pseudo code to help you understand the strategy here.
To clarify, I'll name your first menu as mainMenu and the second as convMenu.
You have already implemented the good strategy for the convMenu. The idea is to make a loop, and exist only when the user tells you so. What you are missing is to do the same for the mainMenu and to think the convMenu as a sub-menu of the mainMenu. Which means when you are in the convMenu you are not outside of the mainMenu.
//That's pseudo code
do {
displayMainMenu();
readUserInput();
switch(userInput) {
case 1 :
//here you put your convMenu
do {
displayConvMenu();
readUserInput();
switch(userInput) {
case 1, 2, 3 :
doConvertion();
case 4 :
exitConvMenu = true;
default :
//wrong input display a message and loop
}
} while(!exitConvMenu)
case 2:
exitMainMenu = true;
default :
//wrong input display a message and loop
}
} while(!exitMainMenu)
Breaking down the code in little pieces usually makes it easier to understand and debug. Divide and conquer! Here is how I would do it:
First create a Menu class which represent a menu, it can print the Menu on screen and check that a choice is valid:
class Menu {
private String title;
private List<String> choices;
private String prompt;
public Menu(String title, List<String> choices, String prompt) {
this.title = title;
this.choices = choices;
this.prompt = prompt;
}
public boolean isChoiceAllowed(int i) {
return i > 0 && i <= choices.size();
}
public void show() {
System.out.println(title);
for(int i=0; i<choices.size(); i++) {
System.out.println(" " + (i+1) + ". " + choices.get(i));
}
System.out.println(prompt);
}
}
Then define your 2 menus:
private static final Menu mainMenu = new Menu(
"Main Menu",
Arrays.asList("Enter Distance", "Quit the program"),
"Please enter your choice");
private static final Menu conversionMenu = new Menu(
"Conversion Menu",
Arrays.asList("Convert to kilometers", "Convert to inches", "Convert to feet", "Return"),
"Please enter your choice");
Have a method to read the user input:
private static int readUserInput() {
int input = keyboard.nextInt();
// Check if input is valid, if not call back readUserInput() until a valid input is given
if(!currentMenu.isChoiceAllowed(input)) {
System.out.println("Please Enter a valid option");
return readUserInput();
}
return input;
}
Have a method to handle choices in the Main Menu
private static void handleMainMenuChoice(int choice) {
switch (choice) {
case 1:
System.out.println("\nEnter a Distance in Meters:");
meters = keyboard.nextDouble();
// Set the current menu to be the Conversion Menu
currentMenu = conversionMenu;
break;
case 2:
quitProgram();
break;
}
}
Have a method to handle choices in the Conversion Menu
private static void handleConversionMenuChoice(int choice) {
switch (choice) {
case 1:
showKilometers(meters);
break;
case 2:
showInches(meters);
break;
case 3:
showFeet(meters);
break;
case 4:
// Set the current menu to be the Main Menu
currentMenu = mainMenu;
break;
}
}
Have a method to handle the user choice and dispatch it to the appropriate method above:
private static void handleChoice(int choice) {
if(currentMenu == mainMenu) {
handleMainMenuChoice(choice);
} else {
handleConversionMenuChoice(choice);
}
}
Then tie it up together:
private static double meters;
private static Menu currentMenu;
private static Scanner keyboard = new Scanner(System.in);
<Insert menu definitions here>
public static void main(String[] args) {
currentMenu = mainMenu;
int choice;
while (true) {
// Show the current menu
currentMenu.show();
// Read the user input
choice = readUserInput();
// Handle the user choice
handleChoice(choice);
}
}
public static void main(String[] args) {
int choice = 0;
int option;
double meters = 0;
Scanner keyboard = new Scanner(System.in);
boolean exit = false;
while(!exit) {
System.out.println("\nEnter your choice :Enter Distance.. click1\r\n" +
"\r\n" +
"Quit the program.. click2");
choice = keyboard.nextInt();
switch (choice) {
case 1:
System.out.println("\nEnter a Distance in Meters:");
meters = keyboard.nextDouble();
System.out.println("Conversion Menu\r\n" +
"\r\n" +
"Convert to kilometers .. click1\r\n" +
"\r\n" +
"Convert to inches .. click2\r\n" +
"\r\n" +
"Convert to feet .. click3\r\n" +
"\r\n" +
"Return.... .. click4");
int choice1 = keyboard.nextInt();
switch(choice1) {
case 1:
showKilometers(meters);
break;
case 2:
showInches(meters);
break;
case 3:
showFeet(meters);
break;
case 4:
exit = true;
break;
}
break;
case 2:
exit = true;
break;
default:
break;
}
}
}
I've looked at everything mentioned about this and if I make a do/while loop it will just repeat the selection. If I make them conditionals instead of a switch it gives me "NoSuchElementException: No line found". Now its also giving me a "NoSuchElementException: No line found" even though I am back to a switch. I just want to know what I'm missing in this code that will let the user back out their first selection (while loop) to make a different one. Here is the code:
public class Zoo {
static FileRead fr = new FileRead();
private static final Scanner scnr = new Scanner(System.in);
public static void main(String[] args) throws FileNotFoundException {
while (true) {
int userChoice = menu();
while (userChoice == 1) {
// Select Animal
int animal = animalSelect();
String Name = null;
switch (animal) {
case 1:
Name = "Animal - Lion";
break;
case 2:
Name = "Animal - Tiger";
break;
case 3:
Name = "Animal - Bear";
break;
case 4:
Name = "Animal - Giraffe";
break;
default:
userChoice = menu();
break;
} FileRead.readAnimal(Name);
}
while (userChoice == 2) {
// Select Habitat
int animal = habitatSelect();
String Name = null;
switch (animal) {
case 1:
Name = "Habitat - Penguin";
break;
case 2:
Name = "Habitat - Bird";
break;
case 3:
Name = "Habitat - Aquarium";
break;
default:
userChoice = menu();
break;
}
FileRead.readHabitat(Name);
}
// Exit Program
if (userChoice == 3) {
System.out.println("Thank you!");
System.exit(0);
}
// Error for invalid option
else {
System.out.println("ERROR: Invalid Selection");
}
}
}
private static int habitatSelect() {
// Habitat Menu
System.out.println("Which habitat would you like to monitor?");
System.out.println("1. Penguin Habitat");
System.out.println("2. Bird Habitat");
System.out.println("3. Aquarium");
System.out.println("4. Exit");
int userChoice = Integer.parseInt(scnr.nextLine());
return userChoice;
}
private static int animalSelect() {
// Animal Menu
System.out.println("Which animal would you like to monitor?");
System.out.println("1. Lion");
System.out.println("2. Tiger");
System.out.println("3. Bear");
System.out.println("4. Giraffe");
System.out.println("5. Exit");
int userChoice = Integer.parseInt(scnr.nextLine());
return userChoice;
}
private static int menu() {
// Main Menu
System.out.println("WELCOME! Plese choose from the following");
System.out.println("1. Monitor Animal");
System.out.println("2. Monitor Habitat");
System.out.println("3. Exit");
int userChoice = Integer.parseInt(scnr.nextLine());
return userChoice;
}
}
This all reads a from another file in the package. If that code is needed I will also post it.
Tweak your main method as below
public static void main(String[] args) throws FileNotFoundException {
while (true) {
int userChoice = menu();
switch (userChoice) {
case 1: // only for animals
int animal = animalSelect();
String Name = null;
switch (animal) {
case 1:
Name = "Animal - Lion";
break;
case 2:
Name = "Animal - Tiger";
break;
case 3:
Name = "Animal - Bear";
break;
case 4:
Name = "Animal - Giraffe";
break;
default:
System.out.println("ERROR: Invalid Selection");
break;
}
if (Name != null) // read file only if selection is correct
FileReader.readAnimal(Name);
break;
case 2: // only for habitat
int habitat = habitatSelect();
String habitatName = null;
switch (habitat) {
case 1:
habitatName = "Habitat - Penguin";
break;
case 2:
habitatName = "Habitat - Bird";
break;
case 3:
habitatName = "Habitat - Aquarium";
break;
default:
System.out.println("ERROR: Invalid Selection");
break;
}
if (habitatName != null) // read file only if selection is correct
FileRead.readHabitat(habitatName);
break;
case 3 : // only for exit
System.out.println("Thank you!");
System.exit(0);
default:
System.out.println("ERROR: Invalid Selection");
}
}
}
Thus after each sub-menu, the user is returned to the main menu. As for your exception, for now I have added a null check so that the file is read only if the selection is correct.
Also, note that the above code doesn't contain nested loop which increases the performance and also excludes (the slightly messy) recursive call.
package thecashmachin;
import java.util.Scanner;
public class TheCashMachin {
public static void main(String[] args) {
int pin, proceed2=0, withdraw, dailydraw, Proceed, proceed3 = 0;
double balance;
Scanner pinnumber = new Scanner(System.in);
Scanner proc2 = new Scanner(System.in);
Scanner withd = new Scanner(System.in);
Scanner Next = new Scanner(System.in);
Scanner proc3 = new Scanner(System.in);
balance = 9999.99;
dailydraw = 1000;
System.out.println(
"text .");
System.out.println("1)Proceed");
System.out.println("2)Return Card");
Proceed = Next.nextInt();
switch (Proceed) {
case 1:// Proceed
System.out.println("Please enter your 5 digit pin below.");
Scanner Pin = new Scanner(System.in);
int Pincode = Pin.nextInt();
if (Pincode > 9999 && Pincode < 99999) {
System.out.println("1)Display Balance");
System.out.println("2)Withdraw Cash");
System.out.println("3)Other services");
proceed2 = proc2.nextInt();
} else {
System.err.println(
"text");
}
break;
case 2:// Return Card
System.err.println("text");
break;
default:
System.err.println(
"text");
break;}
switch (proceed2) {
case 1:
System.out.println("Your balance today is: 9999.99");
/*
* so right here the balance is shown and in real life you would have a go back button to display the other options but on my code after the balance is displayed you cant do anything else have to re run the the script i want a code that if selected goes back to the last option*/
break;
case 2:
System.out.println("Amount to withdraw");
withdraw = withd.nextInt();
System.out.println("Please take the cash");
System.out.println("Current Balance" + " " + (balance - withdraw));
System.out.println("Daily withdraw left:" + (dailydraw - withdraw));
if (withdraw > dailydraw) {
System.err.println("text");
}
case 3:
System.out.println("Would you like to;");
System.out.println("1)Order a check");
System.out.println("2)Order a Statement");
proceed3 = proc3.nextInt();
break;
default:
System.out.println("text");
}
switch (proceed3) {
case 1:
System.out.println("Your check has been orderd");
break;
case 2:
System.out.println("Your Statement has been orderd");
break;
}
}
}
DO
BOOLEAN = TRUE;
SWITCH() // WITH THE DIFFERENT CASES
DEFAULT BOOLEAN = FALSE;
WHILE BOOLEAN IS FALSE;
This should do. Use a simple do while loop.
Before entering the switch, your boolean is set to TRUE and if it comes to the default it turns it to FALSE and you loop until the boolean stays TRUE
An easy way I know is just making a Boolean before which is kept same as default..
boolean test = True;
while (test)
{
switch(Proceed)
{
case 1://Proceed
System.out.println("Please enter your 5 digit pin below.");
Scanner Pin=new Scanner(System.in);
int Pincode=Pin.nextInt();
test = false;
break;
case 2://Return Card
System.err.println("Your card is being ejected.\n Please Wait..");
test = false;
break;
default:
System.err.println("Sorry your request could not be processed.\n Please enter the pin again.\n")
// when neither case is true, keeps loop running.
break;
}
}
I'm using try on my code and it says illegal start of type. I'm using switch statements but default: continue; do not agree with each other I keep getting continue outside of loop. With the else statement it says illegal start type. So what can I do about try, continue, and the else statement.
public class Menu {
private Inventory database;
private char menuItem;
private Scanner input;
private char mode;
int code;
public Menu(Inventory database)
{
this.database = database;
menuItem = 'N';
input = new Scanner(System.in);
}
public Menu(MyArrayList database)
{
this.database = database;
menuItem = 'A';
input = new Scanner(System.in);
}
private void showMenu()
{
if(code == 'A'){
System.out.println();
System.out.println("------------------");
System.out.println("Display Movies : D");
System.out.println("Add Movie : A");
System.out.println("Delete Movie : X");
System.out.println("Select Mode : M");
System.out.println("Exit : E");
System.out.println("------------------");
System.out.println();
System.out.print("Please make your selection: ");
}
else
{
System.out.println();
System.out.println("------------------");
System.out.println("Display Movies : D");
System.out.println("Rent a Movie : R");
System.out.println("Reserve a Movie: S");
System.out.println("Select Mode : M");
System.out.println("Exit : E");
System.out.println("------------------");
System.out.println();
System.out.print("Please make your selection: ");
}
}
private void rentMovie(int productID)
{
int index = database.getIndex(productID);
if( index == -1)
{
System.out.println("There is not such a code.");
}
else
{
if( database.getMovie(index).getIsRented())
{
System.out.println("You cannot rent " + database.getMovie(index).getTitle() + ". It is already rented.");
}
else
{
database.getMovie(index).setIsRented(true);
System.out.println("Please take your movie.");
}
}
}
private void reserveMovie(int productID)
{
int index = database.getIndex(productID);
if( index == -1)
{
System.out.println("There is not such a code.");
}
else
{
if( database.getMovie(index).getIsReserved() )
{
System.out.println("You cannot reserve " + database.getMovie(index).getTitle() + ". It is already reserved.");
}
else
{
if( database.getMovie(index).getIsRented())
{
database.getMovie(index).setIsReserved(true);
System.out.println( database.getMovie(index).getTitle() + " is reserved for you." );
}
else
{
System.out.println( database.getMovie(index).getTitle() + " is available. You can rent it if you like.");
}
}
}
}
try{
if(mode == 'A'){
switch(menuItem){
case 'N':
break;
case 'D':
database.print();
showMenu();
menuItem = input.next().charAt(0);
break;
case 'A':
String title;
System.out.println("Enter movie title, then press enter");//movie title,
title= input.nextLine();
System.out.println("Enter movie code, then press enter");//enter movie code,then press enter
code = Integer.parseInt(input.nextLine());
addMovie(title,code);
menuItem ='N';
break;
case 'X':
System.out.println("");
deleteMovie(code);
menuItem ='N';
break;
case 'M':
selectMode();
menuItem = 'N';
case 'E':
System.out.print("Program terminated.");
System.exit(0);
break;
default:
continue;
}
}
}
else
{
public void run(){
int code;
while(true)
{
switch(menuItem)
{
case 'N':
break;
case 'D':
database.print();
showMenu();
menuItem = input.next().charAt(0);
break;
case 'R':
System.out.print("Please enter product code:");
rentMovie( input.nextInt() );
showMenu();
menuItem = input.next().charAt(0);
break;
case 'S':
System.out.print("Please enter product code:");
reserveMovie( input.nextInt() );
showMenu();
menuItem = input.next().charAt(0);
break;
case 'E':
System.out.print("Program terminated.");
System.exit(0);
break;
default :
showMenu();
menuItem = input.next().charAt(0);
}
}
}
}
You've got else following your try block, and that makes no sense. no, just bad indentation.
Where's your catch block, or finally block? It makes no sense to just have try. Also, you can't just declare a function in the middle of that else block.
Basically, I recommend you re-study the "Java Syntax" chapter of whatever guide you're using, because this code is just all kinds of wrong.
A.. Few pointers..
First of all, your Try doesn't have a catch.
Your case statements lack {} blocks.
And you can't create a method within an else block.
Further to answer your Question: Illegal start of type means you haven't initiated the variable. For example "menuItem"
Edit: To further that, default should be break; not continue;
Edit2: And further your second switch contains a boolean as argument...
With your edit, now showing the issue.
Your Try starts outside of a method body.
reserveMovie is closed just before try starts, and as such is not valid.