Program breaks from switch java - java

for some reason, when I enter '1' on the switch menu, nothing happens, but the program doesn't terminate. It's the same with options 2-5. The default option works just fine. Can anyone help me with this? Thanks
Code:
import java.util.Scanner;
public class ConversionTrial {
public static void main(String[] args) {
double pound;
double euro;
double dollars;
double yen;
double rupees;
double poundEuro;
double poundDollars;
double poundYen;
double poundRupees;
int choice;
Scanner input = new Scanner(System.in);
Scanner exchange = new Scanner(System.in);
menu: while(true) {
System.out.println("Please choose an option:");
System.out.println("1. Enter values");
System.out.println("2. Euros (1GBP = 1.28EUR)");
System.out.println("3. Dollars (1GBP = 1.51USD)");
System.out.println("4. Yen (1GBP = 179.80JPY)");
System.out.println("5. Rupees (1GBP = 95.60INR)");
System.out.println("6. Exit");
choice = input.nextInt();
switch(choice){
case -1:
case 6:
break menu;
case 1:
pound = exchange.nextDouble();
System.out.print("Please enter your values you would like to exchange:");
break;
case 2:
pound = exchange.nextDouble();
euro = 1.28;
poundEuro = pound * euro;
System.out.println("Your amounts in Euros are" + poundEuro);
case 3:
pound = exchange.nextDouble();
dollars = 1.51;
poundDollars = pound * dollars;
System.out.println("Your amounts in Dollars are" + poundDollars);
case 4:
pound = exchange.nextDouble();
yen = 1.28;
poundYen = pound * yen;
System.out.println("Your amounts in Yen are" + poundYen);
case 5:
pound = exchange.nextDouble();
rupees = 1.28;
poundRupees = pound * rupees;
System.out.println("Your amounts in Rupees are" + poundRupees);
default:
System.out.println("You must enter an option between 1 and 6!");
}
}
input.close();
exchange.close();
}
}

First, don't create two scanner objects. Just create the one, and use that.
Second, in you options 1-5, you are waiting for input before outputting anything to the user, so that is likely why it seems to not be working. You should add a prompt for the value expected.
Third, you are missing break; at the end of cases 2-5.
Fourth, using a label is generally not the best way to do things. It can end up with some hard to read code. A better way would be to do it would be to have a flag variable, boolean exit = false;. Then, your while loop will loop based on it not being true, while(!exit). And in your case 6:, exit = true;
Fifth, why do you have -1 exit, when it isn't an option given the user? I would remove that.
import java.util.Scanner;
public class ConversionTrial{
public static void main(String[] args) {
double pound;
double euro;
double dollars;
double yen;
double rupees;
double poundEuro;
double poundDollars;
double poundYen;
double poundRupees;
int choice;
Scanner input = new Scanner(System.in);
boolean exit = false;
while(!exit) {
System.out.println("Please choose an option:");
System.out.println("1. Enter values");
System.out.println("2. Euros (1GBP = 1.28EUR)");
System.out.println("3. Dollars (1GBP = 1.51USD)");
System.out.println("4. Yen (1GBP = 179.80JPY)");
System.out.println("5. Rupees (1GBP = 95.60INR)");
System.out.println("6. Exit");
choice = input.nextInt();
switch(choice){
case 6:
exit = true;
break;
case 1:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
break;
case 2:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
euro = 1.28;
poundEuro = pound * euro;
System.out.println("Your amounts in Euros are " + poundEuro);
break;
case 3:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
dollars = 1.51;
poundDollars = pound * dollars;
System.out.println("Your amounts in Dollars are " + poundDollars);
break;
case 4:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
yen = 1.28;
poundYen = pound * yen;
System.out.println("Your amounts in Yen are " + poundYen);
break;
case 5:
System.out.print("Please enter your values you would like to exchange: ");
pound = input.nextDouble();
rupees = 1.28;
poundRupees = pound * rupees;
System.out.println("Your amounts in Rupees are " + poundRupees);
break;
default:
System.out.println("You must enter an option between 1 and 6!");
break;
}
}
input.close();
}
}
Edit: As a bonus, I also noticed that option 1 doesn't actually do anything. Is that intentional? And for cleaner code, I would initialize the values of your conversion variables when you define the variables, instead of each time they are used. You could also use those values in your menu, so they only need to be changed once if the values change.

The menu label is'nt really necessary. Get rid of it, that is bad code smell.
Additionally you miss the break; in all other cases.

Add breaks for every case to exit the switch statement. Not sure if this is the answer you wanted, so let me know if you are looking for something else.

It's like PeterMmm said. You forget the break; in your cases.
Your code is working but i think it does not what u want it to do.
If i press 1 i can type in a double and afterwards it tells me i can type in a value.
But with this value from switch 1 happens just nothing.
The other options i can type in a double and get the exchange in a foreign currency.
What are u planning to do?

just ran your code and I have figured out what you want to do. I am getting the following:
> Please enter your values you would like to exchange:Please choose an
> option:
> 1. Enter values
> 2. Euros (1GBP = 1.28EUR)
> 3. Dollars (1GBP = 1.51USD)
> 4. Yen (1GBP = 179.80JPY)
> 5. Rupees (1GBP = 95.60INR)
> 6. Exit 2
> 1.2 Your amounts in Euros are1.536
But pay attention that you must enter double values.
Also, I insist that you use break to exit the switch for every case existing in your switch.
Regards

When you select an option 1-5, the program then waits for another double (the amount you want to convert), only then does it respond. So select an option, then give it another double value and it should tell you the conversion amount
import java.util.Scanner;
public class ConversionTrial {
public static void main(String[] args) {
double pound;
double euro;
double dollars;
double yen;
double rupees;
double poundEuro;
double poundDollars;
double poundYen;
double poundRupees;
int choice;
Scanner input = new Scanner(System.in);
Scanner exchange = new Scanner(System.in);
menu: while(true) {
System.out.println("Please choose an option:");
System.out.println("1. Enter values");
System.out.println("2. Euros (1GBP = 1.28EUR)");
System.out.println("3. Dollars (1GBP = 1.51USD)");
System.out.println("4. Yen (1GBP = 179.80JPY)");
System.out.println("5. Rupees (1GBP = 95.60INR)");
System.out.println("6. Exit");
choice = input.nextInt();
switch(choice){
case 1:
pound = exchange.nextDouble();
System.out.print("Please enter your values you would like to exchange:");
break;
case 2:
pound = exchange.nextDouble();
euro = 1.28;
poundEuro = pound * euro;
System.out.println("Your amounts in Euros are" + poundEuro);
break;
case 3:
pound = exchange.nextDouble();
dollars = 1.51;
poundDollars = pound * dollars;
System.out.println("Your amounts in Dollars are" + poundDollars);
break;
case 4:
pound = exchange.nextDouble();
yen = 1.28;
poundYen = pound * yen;
System.out.println("Your amounts in Yen are" + poundYen);
break;
case 5:
pound = exchange.nextDouble();
rupees = 1.28;
poundRupees = pound * rupees;
System.out.println("Your amounts in Rupees are" + poundRupees);
break;
case -1:
case 6:
break menu;
default:
System.out.println("You must enter an option between 1 and 6!");
}
}
input.close();
exchange.close();
}
}

Related

How to break out of case and repeat program until user chooses to exit

I have to make a simple calculator in java that calls methods instead of repeating the entire program over and over again. All of my methods work, and it allows the user to make incorrect choices for as long as they want until a correct choice is made. The problem I am having is that it won't break out of a case after the operation is done and the answer is given.
package menuDrivenCalculator;
import java.util.Scanner;
public class MenuDrivenCalculator {
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
int menuOption = getMenuOption();
while (menuOption < 1 || menuOption > 6) {
System.out.println("I'm sorry, " + menuOption + " is not a valid choice. Please try again.");
menuOption = getMenuOption();
if (menuOption >= 1 && menuOption <= 6) {
break;
}
}
while (menuOption >= 1 && menuOption <= 6) {
switch (menuOption) {
case 1:
System.out.print("What is the first number? ");
double operand1 = getOperand();
System.out.print("What is the second number?");
double operand2 = getOperand();
double add = add(operand1, operand2);
System.out.println("Your answer is: " + add);
break;
case 2:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double subtract = subtract(operand1, operand2);
System.out.println("Your answer is: " + subtract);
break;
case 3:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double multiply = multiply(operand1, operand2);
System.out.println("Your answer is: " + multiply);
break;
case 4:
System.out.print("What is the first number? ");
operand1 = getOperand();
System.out.print("What is the second number?");
operand2 = getOperand();
double divide = divide(operand1, operand2);
System.out.println("Your answer is: " + divide);
break;
case 5:
System.out.print("What is the lower limit? ");
operand1 = getOperand();
System.out.print("What is the upper limit?");
operand2 = getOperand();
double random = random(operand1, operand2);
System.out.println("Your answer is: " + random);
break;
case 6:
System.out.println("Goodbye!");
return;
}
}
}
public static int getMenuOption() {
System.out.println("Menu");
System.out.println("1. Add");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
System.out.println("5. Generate a random number");
System.out.println("6. Quit\n");
System.out.print("What would you like to do? ");
int menuOption = input.nextInt();
return menuOption;
}
public static double getOperand() {
double operand = input.nextDouble();
return operand;
}
public static double add(double operand1, double operand2) {
double add = (operand1 + operand2);
return add;
}
public static double subtract(double operand1, double operand2) {
double subtract = (operand1 - operand2);
return subtract;
}
public static double multiply(double operand1, double operand2) {
double multiply = (operand1 * operand2);
return multiply;
}
public static double divide(double operand1, double operand2) {
double divide = 0;
if (operand2 == 0) {
divide = Double.NaN;
} else if (operand2 != 0) {
divide = (operand1 / operand2);
}
return divide;
}
public static double random(double operand1, double operand2) {
double random = Math.random() * operand2 + operand1;
return random;
}
}
What is happening is the program prompts the user for input for the same operation over and over again until you manually stop the program from running. I've tried putting the entire thing in different types of loops and nothing has changed.
Since the code for performing the operations is inside a loop (while (menuOption >= 1 && menuOption <= 6)) the program will continue to cycle on the last chosen operation.
You need a loop that includes also the getMenuOption() method so the user can choose another operation.
To do so, instead of having 2 separate loops, you could have just 1 to take care of everything (remember also you could use the default case inside the switch).
Since it seems homework I will not give you the complete solution but if you have other specific doubts let us know.
If you dont want it to repeat over,why put the switch statement in while loop?
Replace 'while' with 'if' in your code

Using intAt for a Switch

I'm struggling to understand why intAt will not work in this program. The goal of program is to simply convert weights for a specific planet.
package weightonotherplanets;
import java.util.Scanner;
public class WeightonOtherPlanets
{
public static void main(String args[]){
System.out.println("What is your weight on the Earth?");
Scanner weightInput = new Scanner(System.in); // Enter your weight
int weight = weightInput.nextInt();
System.out.println("1. Voltar\n2. Krypton\n3. Fertos\n4. Servontos\n"); // Choice of planets
System.out.println(" Selection?");
Scanner selectionChoice = new Scanner(System.in);
int selection = selectionChoice.nextInt();
int select = selection.intAt(0); // This is the problem in the code
switch (select)
{
case '1':
System.out.println("Your weight on Voltor would be " + weight * 0.091);
break;
case '2':
System.out.println("Your weight on Krypton would be " + weight * 0.720);
break;
case '3':
System.out.println("Your weight on Fertos would be " + weight * 0.865);
break;
case '4':
System.out.println("Your weight on Servontos would be " + weight * 4.612);
break;
default:
System.out.println("Please make a selection.");
}
}
}

Java while loop doesn't work as intended

public static void main(String[] args) {
boolean run = true;
while (run) {
Scanner kb = new Scanner(System.in);
System.out.println("Welcome to MetricMan - the best converter in town!");
System.out.println("Choose and option:");
System.out.println("A) Fahrenheit to Celsius");
System.out.println("B) Celcius to Fahrenheit");
System.out.println("C) Miles to Kilometers");
System.out.println("D) Kilometers to Miles");
System.out.println("E) Inches to Centimeters");
System.out.println("F) Centimeters to Inches");
System.out.println("X) Quit the program");
System.out.print("Your choice > ");
String choice = kb.nextLine();
double output; int input;
System.out.print("Enter the number of ");
switch (choice) { // do actions according to the user's choice. convert and display
case "A":
System.out.print("Farenheit > ");
input = kb.nextInt();
output = convertFtoC(input);
System.out.println(input+" farenheit is "+output+" celsius");
break;
case "B":
System.out.print("Celsius > ");
input = kb.nextInt();
output = convertCtoF(input);
System.out.println(input+" celsius is "+output+" farenheit");
break;
case "C":
System.out.print("miles > ");
input = kb.nextInt();
output = convertMtoK(input);
System.out.println(input+" miles is "+output+" kilometres");
break;
case "D":
System.out.print("kilometres > ");
input = kb.nextInt();
output = convertKtoM(input);
System.out.println(input+" kilometres is "+output+" miles");
break;
case "E":
System.out.print("inches > ");
input = kb.nextInt();
output = convertItoC(input);
System.out.println(input+" inches is "+output+" centimeters");
break;
case "F":
System.out.print("centimeters > ");
input = kb.nextInt();
output = convertCtoI(input);
System.out.println(input+" centimeters is "+output+" inches");
break;
case "X":
System.out.println(); System.out.println();
System.out.println("May the odds be ever metric!");
run = false;
break;
}
System.out.println();
kb.nextLine();
}
} // end of main()
there's my main method. As seen in the code, when X has been entered the code should stop running straight away, however its actual output is
...
Your choice > X
Enter the number of
May the odds be ever metric!
there is a "enter the number of " before the actualy intended output and I see no reason for it to be there. Can anyone help fix this?
You are doing System.out.print("Enter the number of "); in every case .So thats why its printing when you entered X so try to put System.out.print("Enter the number of "); inside if(check if string entered is not X) like below
if(!choice.equals("X")){
System.out.print("Enter the number of ");
}
OR
put System.out.print("Enter the number of "); inside each switch case where you want it as pointed by #Tanmoy
That line occurs every time you go through the loop. One way to fix this is to make that call in each case statement except for X.
In your code
System.out.print("Enter the number of ");
switch (choice) { // do actions according to the user's choice. convert and display
so you are basically printing this before switch, so the output is expected, you may want to call inside cases rather outside switch, when its x, just quit.

Calculator with continuous input in java

I'm trying to make a calculator where the person can continue to put in numbers like "2+4*7-1" until they press = and then they will get the answer, and I have no idea how to even start. I know how to make a calculator with just 2 numbers but not how to have the user giving new numbers all the time. If anyone have any tips/code I could look at that would help a lot.
Check this Creating a Calculator using JFrame , and this is a step to step tutorial
yes yes i know that i am replying after 2 years but still maybe it will come in handy to other ppl in the future.
its a simple console code no gui.
So here's how i did it on eclipse
import java.util.Scanner;
public class Adv_calc {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int option;
double num1, num2, result;
result = 0;
do {
System.out.println("Welcome to The Calculator app");
System.out.println("Please Choose an option");
System.out.println("1) Add");
System.out.println("2) Subtract");
System.out.println("3) Multiply");
System.out.println("4) Continue");
System.out.println("5) Exit");
System.out.print("Option :: ");
option = sc.nextInt();
switch (option) {
case 1: {
System.out.println("Addition Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 + num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 2: {
System.out.println("Subtraction Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 - num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 3: {
System.out.println("Multiplication Calculator");
System.out.print("Kindly Enter the FIRST Number :: ");
num1 = sc.nextDouble();
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = num1 * num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 4: {
System.out.println("Please Choose an option");
System.out.println("1) Add");
System.out.println("2) Subtract");
System.out.println("3) Multiply");
System.out.print("Option :: ");
option = sc.nextInt();
switch (option) {
case 1: {
System.out.println("Addition Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result + num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 2: {
System.out.println("Subtraction Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result - num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
case 3: {
System.out.println("Multiplication Calculator");
System.out.print("Kindly Enter the SECOND Number :: ");
num2 = sc.nextDouble();
result = result * num2;
System.out.print("The Result is :: ");
System.out.println(result);
break;
}
}
break;
}
case 5: {
System.out.println("Thank you for using my program :: ");
System.out.println("Program will now exit ");
System.exit(0);
}
}
} while (option != 5);
}
}

Java scanner not returning multiple word variable

This section of my code is not working as I need it to. I need to Select from the menu either A, B, or X and then input a small sentence for the description (multiple) a figure for the cost. I can input all the data perfectly and it returns to the menu (only exiting once inputing X). But the return string for the variable damageDesc is only grabbing the first word prior to the space. I've tried this all night and still can't get it to return all words in the string. Here's the code if anyone can help. Thanks!
do {
System.out.println("Damage Repair / Traffic Infringement Data Entry "+
"Menu");
System.out.println("-------------------------------------------------"+
"---");
System.out.println();
System.out.println("A - Record Damage Repair Details");
System.out.println("B - Record Traffic Infringement Details");
System.out.println("X - Exit");
System.out.println();
System.out.print("Enter your selection (A/B/X):");
menuOption = console.next().charAt(0);
switch (menuOption) {
case 'A':
case 'a':
System.out.print("Enter description of damage repair: ");
damageDesc = console.next();
console.nextLine();
System.out.print("Enter repair cost: ");
damageCost= console.nextDouble();
//Place damage description and cost on seperate lines
damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, damageCost);
//Also could have used the below
//damageDescFinal = (damageDescFinal+"- "+damageDesc+
// " ($"+damageCost+")"+"\n");
//All damage costs added together
damageCostFinal = damageCostFinal + damageCost;
System.out.println();
break;
case 'B':
case 'b':
System.out.print("Enter details of traffic infringement: ");
fineDesc = console.next();
console.nextLine();
System.out.print("Enter traffic fine amount: ");
fineCost = console.nextDouble();
//Set fine description and cost on seperate lines
fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc, fineCost);
//Also could have used the below
//fineDescFinal = fineDescFinal+"- "+fineDesc+
// " ($"+fineCost+")"+"\n";
//All fine costs added together
fineCostFinal = fineCostFinal + fineCost;
System.out.println();
break;
case 'X':
case 'x':
//Exit the damage and fine menu
System.out.print("Exiting data entry menu..."+"\n");
System.out.println();
break;
default:
//Error handling for invalid input
System.out.print("***ERROR*** Invalid Selection!"+"\n");
System.out.println();
break;
}
} while (menuOption != 'X' && menuOption != 'x');
Perhaps, you need to use nextLine() for reading the entire line instead of next
Fixed this myself by changing the Double values to strings and then converted them back to a double after the inputs were finished. This solved the scanner problems I was having. Here's the final code that worked:
do {
System.out.println("Damage Repair / Traffic Infringement Data Entry "+
"Menu");
System.out.println("-------------------------------------------------"+
"---");
System.out.println();
System.out.println("A - Record Damage Repair Details");
System.out.println("B - Record Traffic Infringement Details");
System.out.println("X - Exit");
System.out.println();
System.out.print("Enter your selection (A/B/X):");
menuOption = console.next().charAt(0);
//Switch to allow either upper or lower case menuOption
switch (menuOption) {
case 'A':
case 'a':
System.out.print("Enter description of damage repair: ");
//Clear scanner
console.nextLine();
//Input damage description
damageDesc = console.nextLine();
System.out.print("Enter repair cost: ");
//Input damage cost
damageCost= console.nextLine();
//Convert string fineCost back to a double
//This was input as a String as scanner produces an error
double doubleDamageCost = Double.parseDouble(damageCost);
//Set damage description and cost on seperate lines for each input
damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, doubleDamageCost);
//Concatenate all fine costs for a final value
damageCostFinal = damageCostFinal + doubleDamageCost;
System.out.println();
break;
case 'B':
case 'b':
System.out.print("Enter details of traffic infringement: ");
//Clear scanner
console.nextLine();
//Input fine description
fineDesc = console.nextLine();
System.out.print("Enter traffic fine amount: ");
//Input fine cost
fineCost = console.nextLine();
//Convert string fineCost back to a double
//This was input as a String as scanner produces an error
double doubleFineCost = Double.parseDouble(fineCost);
//Set fine description and cost on seperate lines for each input
fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc, doubleFineCost);
//Concatenate all fine costs for a final value
fineCostFinal = fineCostFinal + doubleFineCost;
System.out.println();
break;
case 'X':
case 'x':
//Exit the damage and fine menu
System.out.print("Exiting data entry menu..."+"\n");
System.out.println();
break;
default:
//Error handling for invalid input
System.out.print("***ERROR*** Invalid Selection!"+"\n");
System.out.println();
break;
}
//Exits menu loop and allow code to continue
} while (menuOption != 'X' && menuOption != 'x');
why not in this case use .ToLowerCase or .ToUperCase? this way you can get a cleaner code
do {
System.out.println("Damage Repair / Traffic Infringement Data Entry "+
"Menu");
System.out.println("-------------------------------------------------"+
"---");
System.out.println();
System.out.println("A - Record Damage Repair Details");
System.out.println("B - Record Traffic Infringement Details");
System.out.println("X - Exit");
System.out.println();
System.out.print("Enter your selection (A/B/X):");
Scanner console = new Scanner(System.in);
menuOption = console.next().toLowerCase().charAt(0);
//Switch to allow either upper or lower case menuOption
switch (menuOption) {
case 'a':
System.out.print("Enter description of damage repair: ");
//Clear scanner
console.nextLine();
//Input damage description
damageDesc = console.nextLine();
System.out.print("Enter repair cost: ");
//Input damage cost
damageCost= console.nextLine();
//Convert string fineCost back to a double
//This was input as a String as scanner produces an error
double doubleDamageCost = Double.parseDouble(damageCost);
//Set damage description and cost on seperate lines for each input
damageDescFinal = String.format(damageDescFinal + "- %s ($%.2f ) \n", damageDesc, doubleDamageCost);
//Concatenate all fine costs for a final value
damageCostFinal = damageCostFinal + doubleDamageCost;
System.out.println();
break;
case 'b':
System.out.print("Enter details of traffic infringement: ");
//Clear scanner
console.nextLine();
//Input fine description
fineDesc = console.nextLine();
System.out.print("Enter traffic fine amount: ");
//Input fine cost
fineCost = console.nextLine();
//Convert string fineCost back to a double
//This was input as a String as scanner produces an error
double doubleFineCost = Double.parseDouble(fineCost);
//Set fine description and cost on seperate lines for each input
fineDescFinal = String.format(fineDescFinal + "- %s ($%.2f ) \n", fineDesc, doubleFineCost);
//Concatenate all fine costs for a final value
fineCostFinal = fineCostFinal + doubleFineCost;
System.out.println();
break;
case 'x':
//Exit the damage and fine menu
System.out.print("Exiting data entry menu..."+"\n");
System.out.println();
break;
default:
//Error handling for invalid input
System.out.print("***ERROR*** Invalid Selection!"+"\n");
System.out.println();
break;
}
//Exits menu loop and allow code to continue
} while (menuOption != 'x');
but you should do more classes like
Public void menu(){
System.out.println("Damage Repair / Traffic Infringement Data Entry "+
"Menu");
System.out.println("-------------------------------------------------"+
"---");
System.out.println();
System.out.println("A - Record Damage Repair Details");
System.out.println("B - Record Traffic Infringement Details");
System.out.println("X - Exit");
System.out.println();
System.out.print("Enter your selection (A/B/X):");
}
and so on, to get a more organized and clean code. segementation is your best friend.

Categories