Edit: Just wanted to say thank you to everyone who've helped so far! I'm new to Java and my professor recommended this site for outside help. It's been great so far. I'm going to take a few moments and work on the code and implement the suggestions I've received. Thanks!
I'm trying to create a program that converts temperatures from degrees Fahrenheit to degrees Celsius, and vice-versa. If the user does not enter the correct unit, the program loops back and asks them to re-enter the unit or enter Q to quit the program. This is what I have so far:
My thinking is to incorporate a loop into the default portion of my switch statement, but I'm not sure which loop to use (maybe for?) that will loop back and ask the user to re enter the unit or Q to quit. Any help is really, really appreciated! I've been stuck on this for hours!
System.out.print("\tEnter 'F' (or 'f') for Fahrenheit or "
+ "'C' (or 'c') for Celcius: ");
f_or_c = console.next();
double fahrenheit = 5*(temperature-32)/9;
double celcius = (9*(temperature)/5)+32;
switch (f_or_c) {
case "C":
case "c":
System.out.println("\n\t" + temperature + " " + " degrees Celcius "
+ "= " + celcius + " degrees Fahrenheit.");
break;
case "F":
case "f":
System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit "
+ "= " + fahrenheit + " degrees Celcius.");
break;
default:
System.out.println("\n\tUnknown units -"
+ "\n\tCannot do calculation -"
+ "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius");
System.out.print("\nEnter 'Q' to quit or " +
"any other character to perform another temperature conversion: ");
break;
Let's think this through logically: you want to loop until the user enters the correct input. So, does this mean that you will know prior to starting the loop how many times the loop should iterate? This is key.
Once you know this information, the correct loop falls out, since:
for loops are used when you know in advance how many times you'll loop.
While loops are for when you don't know this information in advance.
You can go with while loop. There are better ways to do this. But just helping you with your code.
System.out.print("\tEnter 'F' (or 'f') for Fahrenheit or "
+ "'C' (or 'c') for Celcius: ");
f_or_c = console.next();
boolean flag=false;
double fahrenheit = 5*(temperature-32)/9;
double celcius = (9*(temperature)/5)+32;
while(true) {
switch (f_or_c) {
case "C":
case "c":
System.out.println("\n\t" + temperature + " " + " degrees Celcius "
+ "= " + celcius + " degrees Fahrenheit.");
flag=false;
break;
case "F":
case "f":
System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit "
+ "= " + fahrenheit + " degrees Celcius.");
flag=false;
break;
default:
System.out.println("\n\tUnknown units -"
+ "\n\tCannot do calculation -"
+ "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius");
System.out.print("\nEnter 'Q' to quit or " +
"any other character to perform another temperature conversion: ");
flag=true;
break;
}
if(flag)
continue;
break;
}
Put the switch statement into a method, and then call that method upon itself for the default case. It's using recursion, but keep in mind if you use recursion in the future, make sure it doesn't infinitely loop or you'll get a stack overflow error.
I might be changing your way of thinking here, but I would Implement it differently. Wrap a while loop around the switch case. This will make sure the switch case is re-executed with the next user input value. I've included an example of what it could look like. Note that I haven't included the input code as there are many options for you. Also, I haven't included the quit code, it only breaks the loop.
bool repeat = TRUE;
while (repeat)
{
switch (f_or_c)
{
case "C":
case "c":
System.out.println("\n\t" + temperature + " " + " degrees Celcius "
+ "= " + celcius + " degrees Fahrenheit.");
repeat = FALSE;
break;
case "F":
case "f":
System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit "
+ "= " + fahrenheit + " degrees Celcius.");
repeat = FALSE;
break;
case "Q":
***YOUR CODE TO QUIT***
repeat = FALSE;
break;
default:
System.out.println("\n\tUnknown units -"
+ "\n\tCannot do calculation -"
+ "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius");
System.out.print("\nEnter 'Q' to quit or " +
"any other character to perform another temperature conversion: ");
***CODE TO PROMPT USER FOR F_OR_C VALUE***
repeat = TRUE;
break;
}
}
How about a do{}while()?
boolean badInput;
do {
badInput = false;
System.out.print("\tEnter 'F' (or 'f') for Fahrenheit or "
+ "'C' (or 'c') for Celcius: ");
f_or_c = console.next();
double fahrenheit = 5*(temperature-32)/9;
double celcius = (9*(temperature)/5)+32;
switch (f_or_c) {
case "C":
case "c":
System.out.println("\n\t" + temperature + " " + " degrees Celcius "
+ "= " + celcius + " degrees Fahrenheit.");
break;
case "F":
case "f":
System.out.println("\n\t" + temperature + " " + " degrees Fahrenheit "
+ "= " + fahrenheit + " degrees Celcius.");
break;
default:
System.out.println("\n\tUnknown units -"
+ "\n\tCannot do calculation -"
+ "\n\tPlease next time enter either 'F' for Fahrenheit or 'C' Celcius");
System.out.print("\nEnter 'Q' to quit or " +
"any other character to perform another temperature conversion: ");
badInput = true; //we got bad input so we'll continue looping
break;
} while(badInput);
Essentially we set badInput to false at the beginning, assuming it to be good input. If we end up getting bad input, we make badInput true, so we will continue looping.
The general difference between a do-while and a while loop is that do-whiles are used for code that you want to execute at least once. It works well in this case, because you know you want to read input at least once.
In my example, the variable badInput is called a flag. Basically, a flag is just a variable that tracks whether something is "on" or "off". In this case, badInput is tracking whether or not we have bad input. Its a simple "on" "off", "true" "false" scenario.
Related
I added a if statement that parses a entered number into a double to use when I do my calculations in the second if statement block, the program has to run if only numbers where entered and not letters, the program doesn't seem to read the numbers when I entered a double(5.5) but works fine when I enter a int(5) number/numbers.
Scanner numbers = new Scanner(System.in);
Scanner operation = new Scanner(System.in);
double number1 = 0;
double number2 = 0;
String operator;
System.out.print("Enter the operator you would like to choose(+, -, *, /): ");
operator = operation.next();
System.out.print("Enter the first number: ");
String num1 = numbers.nextLine();
System.out.print("Enter your second number: ");
String num2 = numbers.nextLine();
boolean check1 = num1.trim().matches("^[0-9]+$");
boolean check2 = num2.trim().matches("^[0-9]+$");
if (check1 == true && check2 == true){
number1 = Double.parseDouble(num1);
number2 = Double.parseDouble(num2);
}else {
System.out.println("Only enter numbers not letters.");
}
String calculation;
if (operator.equals("+")){
calculation = (number1 + " + " + number2 + " = " + (number1 + number2));
System.out.println(calculation);
}else if (operator.equals("-")){
calculation = (number1 + " - " + number2 + " = " + (number1 - number2));
System.out.println(calculation);
}else if (operator.equals("*")){
calculation = (number1 + " * " + number2 + " = " + (number1 * number2));
System.out.println(calculation);
}else if (operator.equals("/")){
calculation = (number1 + " / " + number2 + " = " + (number1 / number2));
System.out.println(calculation);
}else{
calculation = operator + ":" + " Is not a valid operator!";
System.out.println(calculation);
}
I think its maybe the ( . ) thats the problem,
my output to console
Enter the operator you would like to choose(+, -, *, /): +
Enter the first number: 5.5
Enter your second number: 5.5
Only enter numbers not letters.
0.0 + 0.0 = 0.0
And now the int numbers that works fine.
Enter the operator you would like to choose(+, -, *, /): +
Enter the first number: 5
Enter your second number: 5
5.0 + 5.0 = 10.0
Do not use more than one Scanner object. There is no need for more than one, and this could also have some side effects you don't want to deal with.
Since you are using regular expression and others have suggested other solutions, I decided to show you how to use regular expression to evaluate a floating-point value. The correct expression for this is
[-+]?[0-9]*\\.?[0-9]+
or better yet...
[-+]?\\d*\\.?\\d+
This expression evaluates a value that may contain an optional sign (positive or negative) followed by zero or more digits, then followed by optional decimal point and an unlimited number of decimal digits. However, if the decimal point is used, at least one digit must be present. In a case like +2., only the +2 is matched.
The code below is your version of the solution, slightly modified:
public static void main(String[] args) {
final String REGEX = "[-+]?[0-9]*\\.?[0-9]+";
Scanner scanner = new Scanner(System.in);
double number1 = 0;
double number2 = 0;
String operator;
System.out.print("Enter the operator you would like to choose(+, -, *, /): ");
operator = scanner.next();
System.out.print("Enter the first number: ");
String num1 = scanner.next();
System.out.print("Enter your second number: ");
String num2 = scanner.next();
scanner.close();
boolean check1 = num1.trim().matches(REGEX);
boolean check2 = num2.trim().matches(REGEX);
if (check1 && check2) {
number1 = Double.parseDouble(num1);
number2 = Double.parseDouble(num2);
}else {
System.out.println("Only enter numbers not letters.");
}
String calculation;
switch (operator) {
case "+":
calculation = (number1 + " + " + number2 + " = " + (number1 + number2));
break;
case "-":
calculation = (number1 + " - " + number2 + " = " + (number1 - number2));
break;
case "*":
calculation = (number1 + " * " + number2 + " = " + (number1 * number2));
break;
case "/":
calculation = (number1 + " / " + number2 + " = " + (number1 / number2));
break;
default:
calculation = operator + ":" + " Is not a valid operator!";
break;
}
System.out.println(calculation);
}
The main differences are that I used only one Scanner object and that I replaced your nested if/else with a switch.
Below is a sample run
Enter the operator you would like to choose(+, -, *, /): +
Enter the first number: 5.5
Enter your second number: -7.2
5.5 + -7.2 = -1.7000000000000002
Other recommended improvements are:
Loop until a valid operator is provided. There is no reason to wait until all inputs are provided to then determine the calculation cannot be done due to invalid operator.
Do something similar to #1 above for each number.
Use BigDecimal the calculation.
Format the result to show however many decimal values you want to show.
The regular expression "^[0-9]+$" only evaluates to true for integers, because it checks whether the string consists of 0 - 9. If you want to check for doubles as well, this might help: https://www.baeldung.com/java-check-string-number
The first suggestion there is probably the easiest. Surround Double.parseDouble with a try catch, because it will give you an NumberFormatException if its not a number.
hello I wrote a program to convert one temperature from one to another using switch.
the default does not fuction:
When i enter a valid option it works perfectly fine, but when i choose invalid choice it goes the default outputting the lines: You enter invalid choice, please choose again and stop workings without allowing me to choose another choice.
earlier when I tested it, the default allowed me to choose another option.
import java.util.Scanner;
public class Tempature {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
double fahrenheit,celcius,kelvin;
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin");
String word = scan.nextLine();
switch(word){
case "f": System.out.println("Enter Fahrenheit temperature: ");
fahrenheit=scan.nextDouble();
celcius = (fahrenheit-32) * 5/9;
kelvin = (fahrenheit + 459.67) * 5/9;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
case "c": System.out.println("Enter the Celcius temperature: ");
celcius=scan.nextDouble();
fahrenheit = (celcius*9)/5 + 32;
kelvin = celcius + 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
case "k": System.out.println("Enter the Kelvin temperature: ");
kelvin=scan.nextDouble();
fahrenheit = 1.8*(kelvin - 273.15) + 32;
celcius = kelvin - 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
default: System.out.println("You enter invalid choice, please choose again");
}
scan.close();
}
}
This could help you.
add a while loop and read the input of user in default and it will work.
When user puts q he/she will exit the program.
class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double fahrenheit,celcius,kelvin;
// changed
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
String word = scan.next();
//added
while(!word.equals("q")) {
switch(word){
case "f": {
System.out.println("Enter Fahrenheit temperature: ");
fahrenheit=scan.nextDouble();
celcius = (fahrenheit-32) * 5/9;
kelvin = (fahrenheit + 459.67) * 5/9;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
case "c":{
System.out.println("Enter the Celcius temperature: ");
celcius=scan.nextDouble();
fahrenheit = (celcius*9)/5 + 32;
kelvin = celcius + 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
case "k": {
System.out.println("Enter the Kelvin temperature: ");
kelvin=scan.nextDouble();
fahrenheit = 1.8*(kelvin - 273.15) + 32;
celcius = kelvin - 273.15;
System.out.println("" + celcius + " C");
System.out.println("" + fahrenheit + " F");
System.out.println("" + kelvin + " K");
break;
}
default: {
System.out.println("You enter invalid choice, please choose again");
// added
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq.Quit");
word = scan.next();
break;
}
}// end of swtich
// checking for exit
if(!word.equals("q")) {
System.out.println("Choose type of temperature:\nf. Fahrenheit\nc. Celcius\nk. Kelvin\nq. Quit");
word = scan.next();
}
}// end of while
System.out.println("Exited");
}
}
Output:
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
f
Enter Fahrenheit temperature:
45
7.222222222222222 C
45.0 F
280.3722222222222 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
c
Enter the Celcius temperature:
45
45.0 C
113.0 F
318.15 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
k
Enter the Kelvin temperature:
45
-228.14999999999998 C
-378.66999999999996 F
45.0 K
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q. Quit
cdvd
You enter invalid choice, please choose again
Choose type of temperature:
f. Fahrenheit
c. Celcius
k. Kelvin
q.Quit
q
Exited
I'm having trouble resolving how to take an input based of two possible options and returning a value for both options.
example:
What temperature system do you use: celsius or Fahrenheit
(after selection is made, in this case celsius, take answer and convert to Fahrenheit)
Output both temperatures
I've tried 'if else' and 'switch' statement but the when I make a selection say Celsius it will convert both
System.out.println("Select which temperature scale you currently use, (c)elsius or (f)ahrenheit: ");
temperatureScale = scannerIn.next();
System.out.println("Temp in C is: " + temperatureInC);
*/
if (temperatureScale.equals("c")) {
System.out.println("Please enter the temperature in Celsius: ");
temperatureInC = scannerIn.nextDouble();
}
else {
System.out.println("Please enter the temperature in Fahrenheit: ");
temperatureInF = scannerIn.nextDouble();
}
tempInCelsiusConvFromFahrenheit = (temperatureInF - 32) * 5/9;
tempInFahrenheitConvFromCelsius = (temperatureInC * 9/5) + 32;
averageQuizScore = ((quiz1 + quiz2 + quiz3)/3);
System.out.println("*** Thank You ***");
System.out.println("Student EMPLID:" + studentID);
System.out.println("Quiz 1 Score: " + quiz1);
System.out.println("Quiz 2 Score: " + quiz2);
System.out.println("Quiz 3 Score: " + quiz3);
System.out.println("Average quiz score: " + df2.format(averageQuizScore));
System.out.println("Age in months: " + age * 12);
System.out.println("Age in years: " + age);
System.out.println("Temperature in Celsius: " + tempInCelsiusConvFromFahrenheit + '°');
System.out.println("Temperature is Fahrenheit: " + tempInFahrenheitConvFromCelsius + '°');
}
}
I selected celsius and enter 32. and thought I would get 32 C and 91.4 but I got:
Temperature in Celsius: -17.77777777777778°
Temperature is Fahrenheit: 91.4°
If you trace your program you will understand:
1) You enter a value for Celcius, therefore your program falls into:
if (temperatureScale.equals("c")) {
System.out.println("Please enter the temperature in Celsius: ");
temperatureInC = scannerIn.nextDouble();
}
then you try display a value for temperature in Celsius by:
System.out.println("Temperature in Celsius: " + tempInCelsiusConvFromFahrenheit + '°');
and you try to do it by getting a value for the tempInCelsiusConvFromFahrenheit
variable.
However, you try to fill tempInCelsiusConvFromFahrenheit variable with a value in
tempInCelsiusConvFromFahrenheit = (temperatureInF - 32) * 5/9;
and you try to get this value in "Else Block" that you never got into:
else {
System.out.println("Please enter the temperature in Fahrenheit: ");
temperatureInF = scannerIn.nextDouble();
}
but you never get into there. So it is probably initialized as 0 when you declare it. And (0-32)*5/9 = -17.78°C. So the output is correct.
But your coding design is wrong.
I hope this helps.
Your calculations are correct. If the user inputs celsius don't convert it. In other words temp in celsius is tempuratureInC and not tempInCelsiusConvFromFahrenheit.
I am trying to run a do...while while a condition is not met but for some reason my code keeps looping every time regardless of the value of the condition. I've checked my debugger and the value of the variable is correct and the conditions within the loop are met according to similar logic. For some reason the ! doesn't work. Here is my code
do
{
if(isRaceHorse.equalsIgnoreCase("Y"))
{
System.out.print("Please enter the number of races your horse has been in >> ");
numRaces = input.nextInt();
input.nextLine();
System.out.println("Your horse is named " + name + " it is a " + color + " horse born in " + birthYear + " and it has been in " + numRaces + " races.");
}
else if(isRaceHorse.equalsIgnoreCase("N"))
{
System.out.println("Your horse is named " + name + " it is a " + color + " horse born in " + birthYear + ".");
}
else
{
System.out.println("Please use Y for yes and N for no.");
System.out.print("Is your horse a race horse? Enter Y for yes and N for no >> ");
isRaceHorse = input.nextLine();
}
}while(!(isRaceHorse.equalsIgnoreCase("y")) || !(isRaceHorse.equalsIgnoreCase("n")));
Anybody have any ideas as to why this is not giving me what I want?
!(isRaceHorse.equalsIgnoreCase("y")) || !(isRaceHorse.equalsIgnoreCase("n"))
In English, this means the loop will continue as long as isRaceHorse is not equal to "y" or isRaceHorse is not equal to "n". Since it cannot be equal to both "y" and "n" at the same time, I'm pretty certain you want to use && instead:
!isRaceHorse.equalsIgnoreCase("y") && !isRaceHorse.equalsIgnoreCase("n")
I am writing a program where the user has a choice of 7 movies to buy. The user can choose as many movies up to 7 and than will add up the prices of the movies and than print the total price. I must use arrays in my program. My problem is when the user has already chosen their first movie they are given a choice if they want to buy another movie. I'm confused on how I should code my program by giving the user another choice to choose more than 1 movie. I need help on how I should fix my problem because when I run my program I it won't let me have the choice to choose another movie. Here my code:
import java.util.Scanner;
public class MovieHits {
public static void main(String[] args)
{
//Declare Variables
Scanner keyboard = new Scanner(System.in);
int userChoice = 0;
String choice;
int priceTotal = 0;
int [] number = {1, 2, 3, 4, 5, 6, 7};
String [] Movie = new String [7];
int [] movieCost ={ 5, 4, 3, 6, 4, 4, 3};
Movie [0] = "Iron Man";
Movie [1] = "Transformers";
Movie [2] = "Godzilla";
Movie [3] = "Fast and Furious";
Movie [4] = "Captain America";
Movie [5] = "X Men";
Movie [6] = "Rio";
//Welcome the user
System.out.println("Hello, Welcome to TC Movies OnDemand.");
//Display the listed movies so the user can know with movie they want to watch
System.out.println("\nChoose which movie you want to watch: ");
for ( int index = 0; index < 7; index = index + 1 )
{
System.out.println(number[index]+ ": " + Movie[index]);
System.out.println("\t" + "Price: $" + movieCost[index]);
}
//Switch Statement to give user a menu to choose a movie
userChoice = keyboard.nextInt();
switch (userChoice)
{
case 1:
System.out.println("The movie you have chosen is " + Movie[0] + "\nPrice is: " + "$" + movieCost[0]);
break;
case 2:
System.out.println("The movie you have chosen is " + Movie[1] + "\nPrice is: " + "$" + movieCost[1]);
break;
case 3:
System.out.println("The movie you have chosen is " + Movie[2] + "\nPrice is: " + "$" + movieCost[2]);
break;
case 4:
System.out.println("The movie you have chosen is " + Movie[3] + "\nPrice is: " + "$" + movieCost[3]);
break;
case 5:
System.out.println("The movie you have chosen is " + Movie[4] + "\nPrice is: " + "$" + movieCost[4]);
break;
case 6:
System.out.println("The movie you have chosen is " + Movie[5] + "\nPrice is: " + "$" + movieCost[5]);
break;
case 7:
System.out.println("The movie you have chosen is " + Movie[6] + "\nPrice is: " + "$" + movieCost[6]);
break;
default:
System.out.println("I'm Sorry you did not chose a movie.");
break;
}
//Tell the user if they want to get another movie
System.out.println("Do you want to add another movie. Enter Yes or No");
choice = keyboard.next();
do
{
priceTotal = movieCost[userChoice];
}
while (choice.equalsIgnoreCase("Yes"));
{
}
//Tell the user the total price
}
}
I would clean your code up like this:
String choice = "Yes"; //Set to yes by default.
//Welcome the user
System.out.println("Hello, Welcome to TC Movies OnDemand.");
while(choice.equalsIgnoreCase("Yes")){
//Display the listed movies so the user can know with movie they want to watch
System.out.println("\nChoose which movie you want to watch: ");
for ( int index = 0; index < 7; index = index + 1 ){
System.out.println(number[index]+ ": " + Movie[index]);
System.out.println("\t" + "Price: $" + movieCost[index]);
}
userChoice = keyboard.nextInt();
try{
System.out.println("The movie you have chosen is " + Movie[userChoice] + "\nPrice is: " + "$" + movieCost[userChoice]);
}catch(Exception e){
System.out.println("Sorry, you did not choose a movie.");
}
//Tell the user if they want to get another movie
System.out.println("Do you want to add another movie? Enter Yes or No.");
choice = keyboard.next();
}
This refactors your code, meaning you don't have to have to use a long switch statement for every single movie type you have.
We've used userChoice as the actual index to look for, and the try{}catch{} block means that we still can 'catch' invalid input if the user incorrectly enters a movie to watch.
I originally was using a do-while like you had, but that structure never works for me, so I swapped it for a straight-forward while loop that I'm more familiar with.
do {
//Switch Statement to give user a menu to choose a movie
System.out.println("Choose your movie number:");
userChoice = keyboard.nextInt();
switch (userChoice) {
case 1:
System.out.println("The movie you have chosen is " + Movie[0] + "\nPrice is: " + "$" + movieCost[0]);
break;
case 2:
System.out.println("The movie you have chosen is " + Movie[1] + "\nPrice is: " + "$" + movieCost[1]);
break;
case 3:
System.out.println("The movie you have chosen is " + Movie[2] + "\nPrice is: " + "$" + movieCost[2]);
break;
case 4:
System.out.println("The movie you have chosen is " + Movie[3] + "\nPrice is: " + "$" + movieCost[3]);
break;
case 5:
System.out.println("The movie you have chosen is " + Movie[4] + "\nPrice is: " + "$" + movieCost[4]);
break;
case 6:
System.out.println("The movie you have chosen is " + Movie[5] + "\nPrice is: " + "$" + movieCost[5]);
break;
case 7:
System.out.println("The movie you have chosen is " + Movie[6] + "\nPrice is: " + "$" + movieCost[6]);
break;
default:
System.out.println("I'm Sorry you did not chose a movie.");
break;
}
//Tell the user if they want to get another movie
System.out.println("Do you want to add another movie. Enter Yes or No");
choice = keyboard.next();
priceTotal = movieCost[userChoice];
} while (choice.equalsIgnoreCase("Yes"));
}
You should use the do-while loop as follows:
In the do block, put the code that you would like to happen
This should be everything between (and including) "Choose the movie" and retrieving user's choice for repetition.
This means that all of that code is guaranteed to occur once, and will occur again if while condition is met.
The code block for the while condition is above it, and thus is terminated with a semi-colon.
You used do-while loop in wrong place in wrong way.Change Your code to this :
do{
try{
userChoice = keyboard.nextInt();
} catch(Execption e){
System.out.Println("Error in input!");
}
switch (userChoice)
{
case 1:
System.out.println("The movie you have chosen is " + Movie[0] + "\nPrice is: " + "$" + movieCost[0]);
break;
case 2:
System.out.println("The movie you have chosen is " + Movie[1] + "\nPrice is: " + "$" + movieCost[1]);
break;
case 3:
System.out.println("The movie you have chosen is " + Movie[2] + "\nPrice is: " + "$" + movieCost[2]);
break;
case 4:
System.out.println("The movie you have chosen is " + Movie[3] + "\nPrice is: " + "$" + movieCost[3]);
break;
case 5:
System.out.println("The movie you have chosen is " + Movie[4] + "\nPrice is: " + "$" + movieCost[4]);
break;
case 6:
System.out.println("The movie you have chosen is " + Movie[5] + "\nPrice is: " + "$" + movieCost[5]);
break;
case 7:
System.out.println("The movie you have chosen is " + Movie[6] + "\nPrice is: " + "$" + movieCost[6]);
break;
default:
System.out.println("I'm Sorry you did not chose a movie.");
break;
}
//Tell the user if they want to get another movie
try{
System.out.println("Do you want to add another movie. Enter Yes or No");
choice = keyboard.nextLine();
} catch(Execption e){
System.out.Println("Error in input!");
}
priceTotal = movieCost[userChoice];
} while (choice.equalsIgnoreCase("Yes"));