I'm trying to print out different variables I've already declared in different switch cases, they're all in one switch block in a for loop but the compiler isn't allowing me use the variables. Giving me a "cannot find symbol" error.
Here's the code:
public static void main(String[] args) {
double hammer = 3.25, nails = 5.25, paint = 4.75, paintBrush = 2.25, balance = 50.00;
Scanner input = new Scanner(System.in);
System.out.println("Local Hardware Point of Sales");
System.out.println("\t::MENU::");
System.out.println("1. Purchase Items\n2. Display current purchases\n3. Display account balance\n4. Complete transactions and Exit");
System.out.print("Enter choice: ");
int choice = input.nextInt();
switch(choice){
case 1:
for(int i = 0; i <= 2; i++){
System.out.println("\n\tPurchase Items");
System.out.println("What items would you like to purchase?");
System.out.println("\tItems \t\tPrices");
System.out.println("\tHammer\t\t-$3.25\n\tNails\t\t-$3.25\n\tPaint\t\t-$3.25\n\tPaint Brush\t-$3.25");
String item = input.next();
switch (item) {
case "Hammer":
case "hammer":
System.out.println("How many Hammers would you like to purchase?");
int hItem = input.nextInt();
System.out.println("Starting Balance: $" + balance);
double hPrice = hItem*hammer;
System.out.println("Cost for " + hItem + " Hammers: $" + hPrice);
double hBalance = balance - hPrice;
System.out.println("Final Balance: $" + hBalance);
if(hBalance > 50.00){
System.out.println("WARNING: YOU HAVE INSUFFICIENT FUNDS FOR THIS PURCHASE. YOU CANNOT PURCHASE 30 NAILS.");
System.out.println("You will be returned to the main menu...");
}
else{
System.out.println("\nPurchase the "+ hItem+ " Hammers (Y/N)");
String ch = input.next();
if(ch.equals("Yes") || (ch.equals("Y")) || (ch.equals("y")) ){
System.out.println("You purchased "+hItem+"Hammers for "+ hPrice);
}
}
break;
case "Nails":
case "nails":
System.out.println("How many Nails would you like to purchase?");
int nItem = input.nextInt();
System.out.println("Starting Balance: $" + balance);
double nPrice = nItem*nails;
System.out.println("Cost for " + nItem + " Nails: $" + nPrice);
double nBalance = balance - nPrice;
System.out.println("Final Balance: $" + nBalance);
if(nBalance > 50.00){
System.out.println("WARNING: YOU HAVE INSUFFICIENT FUNDS FOR THIS PURCHASE. YOU CANNOT PURCHASE 30 NAILS.");
System.out.println("You will be returned to the main menu...");
}
else{
System.out.println("\nPurchase the "+ nItem+ " Nails (Y/N)");
String ch = input.next();
if(ch.equals("Yes") || (ch.equals("Y")) || (ch.equals("y")) ){
System.out.println("You purchased "+nItem+" Nails for "+ nPrice);
}
}
break;
case "Paint":
case "paint":
System.out.println("How many Paints would you like to purchase?");
int pItem = input.nextInt();
System.out.println("Starting Balance: $" + balance);
double pPrice = pItem*nails;
System.out.println("Cost for " + pItem + " Paints: $" + pPrice);
double pBalance = balance - pPrice;
System.out.println("Final Balance: $" + pBalance);
if(pBalance > 50.00){
System.out.println("WARNING: YOU HAVE INSUFFICIENT FUNDS FOR THIS PURCHASE. YOU CANNOT PURCHASE 30 NAILS.");
System.out.println("You will be returned to the main menu...");
}
else{
System.out.println("\nPurchase the "+ pItem+ " Paints (Y/N)");
String ch = input.next();
if(ch.equals("Yes") || (ch.equals("Y")) || (ch.equals("y")) ){
System.out.println("You purchased "+pItem+" Paints for "+ pPrice);
}
}
break;
case "Paint Brush":
case "paint brush":
System.out.println("How many Paint Brushes would you like to purchase?");
int pbItem = input.nextInt();
System.out.println("Starting Balance: $" + balance);
double pbPrice = pbItem*nails;
System.out.println("Cost for " + pbItem + " Paint Brushes: $" + pbPrice);
double pbBalance = balance - pbPrice;
System.out.println("Final Balance: $" + pbBalance);
if(pbBalance > 50.00){
System.out.println("WARNING: YOU HAVE INSUFFICIENT FUNDS FOR THIS PURCHASE. YOU CANNOT PURCHASE 30 NAILS.");
System.out.println("You will be returned to the main menu...");
}
else{
System.out.println("\nPurchase the "+ pbItem+ " Paint Brushes (Y/N)");
String ch = input.next();
if(ch.equals("Yes") || (ch.equals("Y")) || (ch.equals("y")) ){
System.out.println("You purchased "+pbItem+" Paint Brushes for "+ pbPrice);
}
}
break;
default:
break;
}
System.out.println("\nMake another purchase? (Y/N)");
String ans = input.next();
if(ans.equals("n"))
System.out.println("\tCurrent Receipt");
System.out.println("Items \t\tQuantity \t\tPrice Per \t\tTotal Price");
System.out.println("Hammer \t\t"+**hItem**+" \t\t\t"+hammer+" \t\t\t"+**hPrice**);
System.out.println("Nails \t\t"+**nItem**+" \t\t\t"+nails+" \t\t\t"+**nPrice**);
System.out.println("Paint \t\t"+**pItem**+" \t\t\t"+paint+" \t\t\t"+**pPrice**);
System.out.println("Paint Brush \t\t"+**pbItem**+" \t\t\t"+paintBrush+" \t\t\t"+**pbPrice**);
}
}
}
}
I bolded the variables that are giving the error. P.s I am not allowed to use methods for this project, strictly conditional statements and loops.
Also if anyone doesn't mind, I'm trying to get string input for "Paint Brush" but using input.next() has issues with the space, and when I used input.nexLine() it completely ignored the input and ended the program, so had to revert back to next().
Please if anyone can help, much appreciated. Thanks alot
Declare the variables that give you an error outside of the switch-case. Currently their scope is limited to only the inside of the case part, meaning that for the rest of the code (outside of the case) they don't exist.
Related
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
boolean start = true;
while(start)
System.out.printf("%70s %n", " ##### Zoos Australia ##### " + "\n");
System.out.printf("%57s %n", "Main Menu" + "\n");
System.out.printf("%72s %n", "Zoo has the following ticketing options:");
System.out.print("\n");
System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
System.out.printf("%60s %n", "3 = Senior (60+ yrs)");
System.out.println("\n");
String choose1 = "";
String choose2 = "";
String choose3 = "";
String selected = "";
int option = 0;
{
System.out.print("Please select an option: ");
option = input.nextInt();
if (option == 1) {
choose1 = "Child";
selected = choose1;
} else if (option == 2) {
choose2 = "Adult";
selected = choose2;
} else if (option == 3) {
choose3 = "Senior";
selected = choose3;
}
}
// done
System.out.println("\n");
int price = 0;
int tickets = 0;
System.out.print("Enter the number of tickets: ");
tickets = input.nextInt();
if (selected == choose1) {
price = 10;
} else if (selected == choose2) {
price = 20;
} else if (selected == choose3) {
price = 15;
}
System.out.println("\n");
System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");
System.out.println("\n");
int confirm = 0;
System.out.print("Press 1 to confirm purchase: ");
confirm = input.nextInt();
if (confirm != 1) {
System.out.print("Incorrect Key. Please return to Main Menu");
System.out.println("\n");
} else {
break;
}
System.out.println("\n");
int total = tickets;
price = total * price;
System.out.print("Total amount for " + selected + " tickets: " + "$" + price);
System.out.println("\n");
String pick = "";
System.out.print("Do you wish to continue: ");
input.next();
System.out.println("\n");
if (pick == "no") {
System.out.print("Total amount payable is: " + "$" + price);
System.out.println("\n");
System.out.print("Have a nice day!");
System.out.println("\n");
}}}
Trying to do this at the end of the program where user is asked "Do you wish to continue" using a method or something but cant get it to work. Either the program returns to main menu only or the program ends and displays the total message "Total amount payable..." etc. I have tried using while with continue and break. Using boolean with true and false. But no luck. Thank you anyone that may be able to clear this up for me please.
First, you have to assign the users's input to a variable: pick = input.next(). After that, the problem is that you compare the user's input string with a "no" string by using == operator. When comparing reference types (objects) (and String is an object), in most cases == operator gives you an unpredictable result, because it compares the reference (address of an object in memory) and not the actual content. Please remember, that you always have to use the .equals() method instead. You also have to break from your loop, when the user's input is "no".
There is plenty of material concerning this issue. You can check, for instance, this one How do I compare strings in Java?
P.S. I quickly looked at the rest of your code and put some additional comments, which might help you to improve it. Good luck with learning Java!
Scanner input = new Scanner(System.in);
// boolean start = true; you don't need this line
while(true) { // 'true' condition makes it an infinite loop until you use break
// You also have to surround your while loop with curly braces,
// otherwise you fall into an infinite loop
System.out.printf("%70s %n", " ##### Zoos Australia ##### \n");
System.out.printf("%57s %n", "Main Menu\n");
System.out.printf("%72s %n", "Zoo has the following ticketing options: \n");
System.out.printf("%59s %n", "1 = Child (4-5 yrs)");
System.out.printf("%59s %n", "2 = Adult (18+ yrs)");
System.out.printf("%60s %n", "3 = Senior (60+ yrs)\n");
String choose1 = "";
String choose2 = "";
String choose3 = "";
String selected = "";
int option = 0;
System.out.print("Please select an option: ");
option = input.nextInt();
if (option == 1) {
choose1 = "Child";
selected = choose1;
} else if (option == 2) {
choose2 = "Adult";
selected = choose2;
} else if (option == 3) {
choose3 = "Senior";
selected = choose3;
}
System.out.println(); // "\n" is a redundant argument
int price = 0;
int tickets = 0;
System.out.print("Enter the number of tickets: ");
tickets = input.nextInt();
if (selected.equals(choose1)) { // you should never compare strings with == operator! Always use .equals() instead
price = 10;
} else if (selected.equals(choose2)) {
price = 20;
} else if (selected.equals(choose3)) {
price = 15;
}
System.out.println();
System.out.print("You are purchasing " + tickets + " " + selected + " tickets at " + "$" + price + " each!");
System.out.println();
int confirm = 0;
System.out.print("Press 1 to confirm purchase: ");
confirm = input.nextInt();
if (confirm != 1) {
System.out.print("Incorrect Key. Please return to Main Menu");
System.out.println("\n");
} else {
//break; you cannot use 'break' in the if statement! You have to figure out another way, how to handle an invalid input
}
System.out.println();
int total = tickets;
price = total * price;
System.out.print("Total amount for " + selected + " tickets: " + "$" + price);
System.out.println();
String pick = "";
System.out.print("Do you wish to continue: ");
pick = input.next(); // you have to assign the input to a variable
System.out.println();
if (pick.equals("no")) { // You have to ALWAYS use .equals() when comparing Strings or any other reference types! == works correctly only with primitive types
System.out.print("Total amount payable is: " + "$" + price);
System.out.println();
System.out.print("Have a nice day!");
System.out.println();
break; // you need to break from the loop in the end
}
}
}
this project i use do while loop with switch case to check the input case is not match or not. i run the code but the result not what i wanted. what i expect is if the user type the wrong case, the do while loop will loop back to the input where user need to enter the case.
here is the code
package vending.machine;
import java.util.Scanner;
import java.util.*;
import java.util.ArrayList;
import static vending.machine.adddrinks.drinksList;
public class VendingMachine {
public static void main (String []args){
Scanner sc= new Scanner(System.in);
double money;
double total;
double balance;
do{
System.out.println("\nPlease insert money:");
money = sc.nextDouble();
if(money < 1.2){
System.out.println("Not enough money");
}
}while(money < 1.2);
System.out.println("What drinks are you looking for");
adddrinks.showDrinks();
adddrinks.viewDrinks();
System.out.print("Select: 1 or 2 or 3 or 4\n");
int select=sc.nextInt();
do{
switch(select){
case 1:{
total = adddrinks.drinksList.get(0).getdrinkPrice();
balance = money - total;
System.out.println("Here is your balance: " + balance);
break;
}
case 2:{
total = adddrinks.drinksList.get(1).getdrinkPrice();
balance = money - total;
System.out.println("Here is your balance: " + balance);
break;
}
case 3:{
total = adddrinks.drinksList.get(2).getdrinkPrice();
balance = money - total;
System.out.println("Here is your balance: " + balance);
break;
}
case 4:{
total = adddrinks.drinksList.get(3).getdrinkPrice();
balance = money - total;
System.out.println("Here is your balance: " + balance);
break;
}
default:{
System.out.println("Invalid");
break;
}
}
}while(select<5);
}
}
here is the result
enter image description here
From what I understood from your code. When you are giving the input as 5 it is giving invalid.
After that it will go to the while statement and check the condition there. If you are inside the switch case and select any random case It will show you invalid. After that depending upon the number that you have entered.
If the number is less than 5, It will again go to switch case.
As it doesn't make sense as If you continue to provide correct input to it. The code will continue to execute making the balance going in the negative. this condition should be changed to
while(balance>1.2)
assuming that it is minimum amount that is necessary to buy a drink. This will check the condition after every drink and will hopefully do what you were hoping.
On side Note : Make your code modular.
You need to loop over your input, i was so free to improve your code a bit (sorry I do not like repetations):
private static void main10(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("What drinks are you looking for");
adddrinks.showDrinks();
adddrinks.viewDrinks();
int select = 0;
double balance = 0;
boolean running = true;
while (running) {
if (sc.hasNextInt()) {
select = sc.nextInt();
if (0 < select && select <= adddrinks.drinksList.size()) {
double price = adddrinks.drinksList.get(select - 1).getdrinkPrice();
if (balance < price) {
System.out.println("Not enough money, " + select + " costs " + price);
} else {
balance -= price;
System.out.println("You choosed " + select + " , you will find it in the dispenser");
}
} else {
System.out.println("Invalid input, please retry");
}
} else if (sc.hasNextDouble()) {
balance += sc.nextDouble();
} else {
String input = sc.next();
if (input == "q") {
running = false;
if (0 < balance)
System.out.println("please don't forget your change with amount of: " + balance);
System.out.println("Have a nice day, happy to see you again");
break;
} else if (input == "h") {
System.out.println("What drinks are you looking for");
adddrinks.showDrinks();
adddrinks.viewDrinks();
} else {
System.out.println("Invalid input, please retry");
}
}
System.out.println("Your balance is: " + balance);
System.out.println(
"please chouce your product (e.g 2), enter coins (e.g 2.0), click on 'h' to show product list or click on 'q' to get your change");
}
}
Hi guys i am having an issue with how to do this, i have googled it but its not making much sense.
i need to do this;
The program asks the user if they wish to continue.
If Yes is selected, it will return to the Main menu.
If No is selected, Total Amount Payable will be
displayed and then the program will terminate
int option, quantity, confirm;
float childTotal;
float adultTotal;
float seniorTotal;
final double childCost = 18;
final double adultCost = 36;
final double seniorCost = 32.50;
char resume;
Scanner input = new Scanner(System.in);
System.out.println("1 = Child (4-6 yrs)");
System.out.println("2 = Adult (16+ yrs)");
System.out.println("3 = Senior (60+ yrs)" + "\n");
System.out.println("Enter your option:" );
option=input.nextInt();
switch (option) {
case 1:
System.out.println("Enter total No of tickets for Child:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " child tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
case 2:
System.out.println("Enter total No of tickets for Adult:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " adult tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
default:
System.out.println("Enter total No of tickets for Senior:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " senior tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
}
if (confirm !=1) {
System.out.println("Incorrect key! User to go back to main menu");
}
System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);
if (resume == 'y' || resume == 'Y') {
} else {
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break;
case 2:
adultTotal=(int) ((double) quantity*adultCost) ;
System.out.println("Total amount for adult tickets $" + adultTotal);
break;
default:
seniorTotal=(int) ((double) quantity*seniorCost);
System.out.println("Total amount for senior tickets $" + seniorTotal);
break;
}
}
Create a Boolean variable set as true.
boolean continueLoop = true;
Add your main logic into a while loop until continue is true
while(continueLoop){
//Do your code here
System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);
if (resume == 'y' || resume == 'Y'){}
else{
//Do Code here
continueLoop=false;
}
} //End while loop.
After the while loop continue with your code. I have changed the condition of resume == y to resume !=y because if the user does not press y the code should stop iterating.
Your code would become
int option, quantity, confirm;
float childTotal;
float adultTotal;
float seniorTotal;
final double childCost = 18;
final double adultCost = 36;
final double seniorCost = 32.50;
boolean continueLoop = true;
char resume;
Scanner input = new Scanner(System.in);
while(continueLoop){
System.out.println("1 = Child (4-6 yrs)");
System.out.println("2 = Adult (16+ yrs)");
System.out.println("3 = Senior (60+ yrs)" + "\n");
System.out.println("Enter your option:" );
option=input.nextInt();
switch (option) {
case 1:
System.out.println("Enter total No of tickets for Child:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " child tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
case 2:
System.out.println("Enter total No of tickets for Adult:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " adult tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
default:
System.out.println("Enter total No of tickets for Senior:" );
quantity=input.nextInt();
System.out.println("You are purchasing " + quantity + " senior tickets");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
}
if (confirm !=1) {
System.out.println("Incorrect key! User to go back to main menu");
}
System.out.println("Do you wish to continue? (Y/N) ");
resume = input.next().charAt(0);
if (resume == 'y' || resume == 'Y') {
}else{
continueLoop = false;
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break;
case 2:
adultTotal=(int) ((double) quantity*adultCost) ;
System.out.println("Total amount for adult tickets $" + adultTotal);
break;
default:
seniorTotal=(int) ((double) quantity*seniorCost);
System.out.println("Total amount for senior tickets $" + seniorTotal);
break;
}
}
}
}
This question already has answers here:
Semicolon at end of 'if' statement
(18 answers)
Closed 6 years ago.
Need help with if else statement in Java. Need the program to say "Sorry, out of stock" when items are at 0. I tried but it wont print out "Sorry, out of stock" Can anyone explain to me how to properly set that up so when the items are at 0 the program will let the user know that the item is out of stock. Thank you.
import java.util.Scanner;
public class VendingMachine {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int Chips = 5;
int Cookies = 4;
int Candies = 3;
double ChipsPrice = 1.25;
double CookiesPrice = 0.85;
double CandiesPrice = 0.95;
Scanner choice = new Scanner(System.in);
Scanner moneyIn = new Scanner(System.in);
while (true) {
double Change = 0;
double Amount = 0;
double Money = 0;
System.out.println("Welcome to the Vending Machine");
System.out.println("Please insert Money");
Amount = moneyIn.nextDouble();
//Make an if statements, such as if moneyIn equals 5 quarters then Amount = 5*0.25
//Ask how many quarters how many nickels how many dimes
System.out.println("What snack would you like?");
System.out.println("Potato Chips: $" + ChipsPrice + " " + Chips + " left");
System.out.println("Cookies: $" + CookiesPrice + " " + Cookies + " left");
System.out.println("Candies: $" + CandiesPrice + " " + Candies + " left");
String which = choice.nextLine();
if (which.equals("Potato Chips")) {
System.out.println("You selected Potato Chips: $" + ChipsPrice + " " + Chips + " left");
if (Amount < ChipsPrice) {
System.out.println("Not enough money inserted");
if (Chips == 0) ;
System.out.println("Sorry, out of stock");
} else {
Chips = Chips - 1;
Change = ChipsPrice - Amount;
System.out.println("Please take your chips ");
System.out.println("Your change is " + Change);
}
} else if (which.equals("Cookies")) {
System.out.println("You selected Cookies: $" + CookiesPrice + " " + Cookies + " left");
Cookies = Cookies - 1;
if (Amount < CookiesPrice) {
System.out.println("Not enough money inserted");
if (Cookies == 0)
System.out.println("Sorry, out of stock");
} else {
Cookies = Cookies - 1;
Change = CookiesPrice - Amount;
System.out.println("Please take your cookies");
System.out.println("Your change is " + Change);
}
} else if (which.equals("Candies")) {
System.out.println("You selected Candies: $" + CandiesPrice + " " + Candies + " left");
if (Amount < CandiesPrice) {
System.out.println("Not enough money inserted");
if (Cookies == 0)
System.out.println("Sorry, out of stock");
} else {
Candies = Candies - 1;
Change = CookiesPrice - Amount;
System.out.println("Please take your candies");
System.out.println("Your change is " + Change);
}
} else {
System.out.println("Please select one of the snacks below");
}
}
}
}
Just to go through this, a few observations:
// It might be simpler to use a "switch" statement here
if (which.equals("Potato Chips")) {
System.out.println("You selected Potato Chips: $"+ChipsPrice+" "+Chips+" left");
if (Amount < ChipsPrice){
System.out.println("Not enough money inserted");
// Remove the semicolon - as written this won't do anything
// Also, this condition shouldn't be here since you're not vending anyway
// Incidentally, many people argue that you should always use curly
// brackets, even around one-line "if" statements like this, precisely
// to prevent errors like this
if (Chips == 0);
System.out.println("Sorry, out of stock");
}
else {
// This can be written as Chips--;
Chips = Chips - 1;
// Should actually be Amount - ChipsPrice;
// If they paid 75 cents for a 25-cent item, the change is 75 - 25 = 50 cents,
// NOT 25 - 75 = -50 cents
Change = ChipsPrice - Amount;
System.out.println("Please take your chips " );
System.out.println("Your change is "+ Change );
}
}
else if (which.equals("Cookies")) {
System.out.println("You selected Cookies: $"+CookiesPrice+" "+Cookies+" left");
// Cookies--
Cookies = Cookies - 1;
if (Amount < CookiesPrice){
System.out.println("Not enough money inserted");
// Should be checked in the "else" statement
if (Cookies == 0)
System.out.println("Sorry, out of stock");
}
else {
// Cookies--
Cookies = Cookies - 1;
// Amount - CookiesPrice
Change = CookiesPrice - Amount;
System.out.println("Please take your cookies");
System.out.println("Your change is "+ Change );
}
}
else if (which.equals("Candies")) {
System.out.println("You selected Candies: $"+CandiesPrice+" "+Candies+" left");
if (Amount < CandiesPrice){
System.out.println("Not enough money inserted");
// Again, you shouldn't check this here given that you won't vend either way
// Also, should be if (Candies == 0), NOT if (Cookies == 0)
if (Cookies == 0)
System.out.println("Sorry, out of stock");
}
else {
// Candies--;
Candies = Candies - 1;
// Should actually be Amount - CandyPrice. You use CookiesPrice instead.
Change = CookiesPrice - Amount;
System.out.println("Please take your candies");
System.out.println("Your change is "+ Change );
}
}
else {
System.out.println("Please select one of the snacks below");
}
One more thing: you're basically doing the same exact thing 3 consecutive times; in situations like that, it's usually better to try to refactor the behavior in question as a method (rather than typing it 3 separate times).
I want to use switch statement after an if statement but I can't and I don't know what is the problem.
public class main {
public static void main (String[] args) {
String input; //To hold user's input.
char selectPackage; //To hold Internet Package
double hourUsage, totalCharges, addCharges; //other variables
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Prompt the user to select a Internet Package.
System.out.print("Which package did you purchase? ( Enter the Package's letter)");
input = keyboard.nextLine();
selectPackage = input.charAt(0);
System.out.print("Please select the amount of hours used.");
input = keyboard.nextLine();
hourUsage = Double.parseDouble(input);
//Display pricing for selected package...
switch (selectPackage)
{
case 'a':
case 'A':
if (hourUsage > 10)
{
addCharges = hourUsage - 10;
totalCharges = (addCharges * 2.0) + 9.95;
System.out.println("You have used " + hourUsage + " hours and your total is $" +
totalCharges + " per month. ");
}
else
System.out.println("Your total is $9.95 per month.");
break;
case 'b':
case 'B':
if (hourUsage > 20 )
{
addCharges = hourUsage - 20;
totalCharges = (addCharges * 1.0) + 13.95;
System.out.println("You have used " + hourUsage + " and your total is $" + totalCharges + " per month.");
}
else
System.out.println("Your total is $13.95 per month.");
break;
case 'c':
case 'C':
System.out.println("Your total is $19.95 per month.");
break;
default:
System.out.println("Invalid Choice. Choice A,B,C");
}
}
}
System.out.println("Your total is $19.95 per month.");
}
else
System.out.println("Your total is $19.95 per month.");
}
}
Now I want to use the switch statement for telling user that if he/she chose package "B", he would save 20 dollars.
I have had a look through your code and have made ALOT of edits and improvements, the main issue I found was your use of } in the wrong places. I believe this was because you haven't organised your code very well; in future consider organising your code to make it easier to find errors, below I have corrected your code and have put the last few lines into a comment as I'm not sure why you have them there, if there are any questions about it, just ask me:
public class Test {
public static void main(String[] args) {
char selectPackage; //To hold Internet Package
double hourUsage, totalCharges, addCharges; //other variables
//Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
//Prompt the user to select a Internet Package.
System.out.print("Which package did you purchase? ( Enter the Package's letter)");
char input = keyboard.next().charAt(0);
selectPackage = Character.toUpperCase(input);
System.out.print("Please select the amount of hours used.");
hourUsage = keyboard.nextDouble();
//Display pricing for selected package...
switch (selectPackage) {
case 'A':
if (hourUsage > 10) {
addCharges = hourUsage - 10;
totalCharges = (addCharges * 2.0) + 9.95;
System.out.println("You have used " + hourUsage + " hours and your total is $" + totalCharges + " per month. ");
}
else {
System.out.println("Your total is $9.95 per month.");
}
break;
case 'B':
if (hourUsage > 20 ) {
addCharges = hourUsage - 20;
totalCharges = (addCharges * 1.0) + 13.95;
System.out.println("You have used " + hourUsage + " and your total is $" + totalCharges + " per month.");
}
else{
System.out.println("Your total is $13.95 per month.");
}
break;
case 'C':
System.out.println("Your total is $19.95 per month.");
break;
default:
System.out.println("Invalid Choice. Choice A,B,C");
}
/**System.out.println("Your total is $19.95 per month.");
System.out.println("Your total is $19.95 per month.");
**/
}
}