I'm trying to make it so when a user inputs an option its not case sensitive and they don't have to type the full option. I cant figure out how to do it.
package com.unspoken;
import java.util.Scanner;
import java.awt.*;
public class Main {
public static void main(String[] args) {
String play = "Play a game";
String internet = "Explore the internet";
String calculator = "Use the calculator";
String quit = "Quit Untouched";
String pickedEvent = "Unpicked";
Scanner scanner = new Scanner(System.in);
System.out.println("Hello, My name is Ghost. What's your name?");
String name = scanner.nextLine().trim();
System.out.println("Hello " + name + ". What would you like to do today?");
while (!pickedEvent.equals("Picked")) {
System.out.println(play);
System.out.println(internet);
System.out.println(calculator);
System.out.println(quit);
pickedEvent = scanner.nextLine();
switch (pickedEvent) {
case "Play a game":
System.out.println("Okay " + name + ", Loading games.");
pickedEvent = "Picked";
break;
case "Explore the internet":
System.out.println("Okay " + name + ", Loading the internet.");
pickedEvent = "Picked";
break;
case "Use the calculator":
System.out.println("Okay " + name + ", Loading calculator.");
pickedEvent = "Picked";
break;
case "Quit Untouched":
System.out.println("Are you sure you want to quit Untouched " + name + "?");
String quitAnswer = scanner.nextLine().trim();
if(quitAnswer.equalsIgnoreCase("Yes")){
System.out.println("Okay goodbye " + name + ", Have a nice day.");
break;
}else if(quitAnswer.equalsIgnoreCase("No")){
System.out.println("What would you like to do today " + name + "?");
continue;
}
}
}
}
}
try something like this
switch (pickedEvent.toUpperCase()) // changing to uppercase
{
case "PLAY A GAME": // select on uppercase
// ...
}
You can assign numbers to identify the tasks
String play = "1.Play a game";
String internet = "2. Explore the internet";
String calculator = "3. Use the calculator";
String quit = "4. Quit Untouched";
and ask user to enter number instead of typing in complete string
System.out.println("Hello " + name + ". What would you like to do today?, pick number");
and use the numbers in switch case instead of string, change pickedEvent to int
int pickedEvent = 0;
while (pickedEvent != 4) {
System.out.println(play);
System.out.println(internet);
System.out.println(calculator);
System.out.println(quit);
pickedEvent = Integer.parseInt(scanner.nextLine());
switch (pickedEvent) {
case 1:
System.out.println("Okay " + name + ", Loading games.");
break;
case 2:
System.out.println("Okay " + name + ", Loading the internet.");
break;
case 3:
System.out.println("Okay " + name + ", Loading calculator.");
break;
case 4:
System.out.println("Are you sure you want to quit Untouched " + name + "?");
String quitAnswer = scanner.nextLine().trim();
if(quitAnswer.equalsIgnoreCase("Yes")){
System.out.println("Okay goodbye " + name + ", Have a nice day.");
break;
}else if(quitAnswer.equalsIgnoreCase("No")){
System.out.println("What would you like to do today " + name + "?");
continue;
}
}
}
Here is a program that selects an option from an array of choices using a case insensitive prefix match. It does not solve your whole problem, but shows how you can do this kind of selection.
public class PartialMatch
{
public static void main (String[] args)
{
PartialMatch app = new PartialMatch ();
app.execute ();
}
private String[][] choices = {{"alpha", "A"}, {"beta", "B"}, {"alphabet", "C"}};
private void execute ()
{
check ("a");
check ("b");
check ("alpha");
}
private void check (String input)
{
String choice = selectChoice (input);
if (choice == null)
{
System.out.printf ("For input '%s' no selection was found %n%n", input);
}
else
{
System.out.printf ("For input '%s' the choice is '%s' %n%n", input, choice);
}
}
private String selectChoice (String input)
{
String result = null;
for (int n = 1; n <= input.length () && result == null; n++)
{
result = findChoice (input, n);
}
return result;
}
private String findChoice (String input, int n)
{
String result = null;
String needle = input.substring (0, n).toLowerCase ();
for (String[] option : choices)
{
String key = option[0];
if (key.length () >= n)
{
if (key.toLowerCase ().equals (input.toLowerCase ()))
{
System.out.printf ("Option %s is an exact match%n", key);
return option[1];
}
if (key.substring (0, n).toLowerCase ().equals (needle))
{
System.out.printf ("Option %s matches at length %s %n", key, n);
if (result != null)
{
System.out.printf ("Key '%s' is ambiguous %n", needle);
return null;
}
result = option[1];
}
}
}
return result;
}
}
I'm learning Java for a week, and now i have a problem, because i want to print all Arraylist in method printArray, but the method don't see getName() and other methods and I don't know how to solve my problem. Thanks a lot for your help.
If you can, please show my what I;m doing wrong.
Class Positions:
public class Positions {
List<Positions> list = new ArrayList<>(15);
int ageAdd;
int IDAdd;
String nameAdd;
int counter;
String name;
int age;
int ID;
public Positions(String name, int age, int ID) {
this.name = name;
this.age = age;
this.ID = ID;
}
public Positions() {
this("", 0, 0);
}
//there are methods:
//adding element
//removing element
//changing values
//etc
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getID() {
return ID;
}
public void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " : " + " NAME: " + list.getName(i) + " AGE: " + list.getAge(i) + " ID: " list.getID(i));
}
}
Class main:
public class ArrayList2
{
public static void main (String[] args)
{
Positions p = new Positions();
System.out.println("----LISTA TABLICOWA-----");
System.out.println("Co chcesz wykonać? ");
System.out.println("1. Dodac element do listy. ");
System.out.println("2. Usunac elemnt z listy.");
System.out.println("3. Wstawić na dowolna pozycje.");
System.out.println("4. Rozmiar listy.");
System.out.println("5. Zmienic wartosc na podanym indeksie.");
System.out.println("6. Wyświetlić listę. ");
while(true) {
System.out.println("podaj pozycję: ");
Scanner ch = new Scanner(System.in);
int choice= ch.nextInt();
switch (choice)
{
case 1:
{
p.addPosition(); //?
break;
}
case 2:
{
p.removePosition();
break;
}
case 3:
{
p.setOnAnyPosition();
break;
}
case 4:
{
p.ArraySizeShow();
break;
}
case 5:
{
p.changePosition();
break;
}
case 6:
{
p.printArray();
break;
}
}
}
}
}
You simply have a problem with the array access:
Instead of using:
public void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " : " + " NAME: " + list.getName(i) ...);
}
}
You should use:
public void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " : " + " NAME: " + list.get(i).getName() ...);
}
}
because you want to get the name of a specific element, and in OO languages, that usually means calling the method on the object itself.
Ijn your example, you have a list that contains objects that contains a name. So if you want to access the name from the list, you need to first get an element then get his name.
HTH.
Try to change method with below code. Hope it would help.
public void printArray() {
for (int i = 0; i < list.size(); i++) {
System.out.println(i + " : " + " NAME: " + list.get(i).getName() + " AGE: " + list.get(i).getAge() + " ID: " list.get(i).getID());
}
}
To loop over your list either use
for (int i = 0; i < list.size(); i++) {
Positions p = list.get(i);
System.out.println(i + " : " + " NAME: " + p.getName() + " AGE: " + p.getAge() + " ID: " + p.getID());
}
or
int index =0;
for (Positions p : list) {
System.out.println(index++ + " : " + " NAME: " + p.getName() + " AGE: " + p.getAge() + " ID: " + p.getID());
}
This question already has answers here:
Unable to get total amount
(2 answers)
Closed 4 years ago.
I have designed a program which will rerun when the user enters "y" when asked if they wish to continue. The problem I am having is once the user enters "n" the program is supposed to display the total amount payable from all ticket options purchased. I have spent a couple of weeks stuck on this problem and am unsure of what to do next. I have only included the bottom part of my code. I have also included a photo to show my problem when the program is run.
here is my code:
package cse1pgx_a2;
import java.util.Scanner;
public class CSE1PGX_A2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int option, quantity, confirm;
float childTotal = 0;
float adultTotal = 0;
float seniorTotal = 0;
float finalTotal = 0;
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("\t"+ "##### Welcome to Zoos Victoria #####");
System.out.println("\t" + "\t" + "MAIN MENU" + "\n");
System.out.println("\t" + "Zoo has the following ticketing options" + "\n");
System.out.println("\t" + "1 = Child (4-6 yrs)");
System.out.println("\t" + "2 = Adult (16+ yrs)");
System.out.println("\t" + "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 at " + childCost + " each!");
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 at " + adultCost + " each!");
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 at " + seniorCost + " each!");
System.out.println("Press 1 to confirm");
confirm=input.nextInt();
break;
}
if (confirm !=1) {
System.out.println("Incorrect key!");
}
OUTER:
while (confirm == 1) {
switch (option) {
case 1:
childTotal=(int) ((double) quantity*childCost) ;
System.out.println("Total amount for child tickets: $" + childTotal);
break OUTER;
case 2:
adultTotal=(int) ((double) quantity*adultCost) ;
System.out.println("Total amount for adult tickets $" + adultTotal);
break OUTER;
default:
seniorTotal=(int) ((double) quantity*seniorCost);
System.out.println("Total amount for senior tickets $" + seniorTotal);
break OUTER;
}
}
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:
finalTotal=(float) ((double) childTotal+adultTotal+seniorTotal) ;
System.out.println("Total amount payable: $ " + finalTotal);
break;
default:
System.out.println("Error");
}
}
}
}
}
I have fixed issues and also updated code for better performance.
package test;
import java.util.Scanner;
public class CSE1PGX_A2 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
final float childCost = 18;
final float adultCost = 36;
final float seniorCost = 32.50F;
boolean continueLoop = true;
Scanner input = new Scanner(System.in);
float childTotal = 0;
float adultTotal = 0;
float seniorTotal = 0;
while (continueLoop) {
int option, confirm=0;
System.out.println("\t ##### Welcome to Zoos Victoria #####");
System.out.println("\t \t MAIN MENU \n");
System.out.println("\t Zoo has the following ticketing options \n");
System.out.println("\t 1 = Child (4-6 yrs)");
System.out.println("\t 2 = Adult (16+ yrs)");
System.out.println("\t 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:");
int quantity = input.nextInt();
childTotal = quantity * childCost;
System.out.println("You are purchasing " + quantity + " child tickets at " + childCost + " each!");
System.out.println("Press 1 to confirm");
confirm = input.nextInt();
if (confirm == 1) {
System.out.println("Total amount for child tickets: $" + childTotal);
}
break;
}
case 2: {
System.out.println("Enter total No of tickets for Adult:");
int quantity = input.nextInt();
adultTotal = quantity * adultCost ;
System.out.println("You are purchasing " + quantity + " adult tickets at " + adultCost + " each!");
System.out.println("Press 1 to confirm");
confirm = input.nextInt();
if (confirm == 1) {
System.out.println("Total amount for adult tickets $" + adultTotal);
}
break;
}
case 3: {
System.out.println("Enter total No of tickets for Senior:");
int quantity = input.nextInt();
seniorTotal = quantity * seniorCost ;
System.out.println("You are purchasing " + quantity + " senior tickets at " + seniorCost + " each!");
System.out.println("Press 1 to confirm");
confirm = input.nextInt();
if (confirm == 1) {
System.out.println("Total amount for senior tickets $" + seniorTotal);
}
break;
}
}
if (confirm != 1) {
System.out.println("Incorrect key!");
}
System.out.println("Do you wish to continue? (Y/N) ");
char resume = input.next().charAt(0);
if (resume != 'y' && resume != 'Y') {
continueLoop = false;
System.out.println("Total amount for child tickets: $" + childTotal);
System.out.println("Total amount for senior tickets $" + seniorTotal);
System.out.println("Total amount for adult tickets $" + adultTotal);
float finalTotal = childTotal + adultTotal + seniorTotal ;
System.out.println("Total amount payable: $ " + finalTotal);
}
}
}
}
Try this code. I hope it helps.
public static void main(String[] args) {
int option, quantity, confirm; //minor change
float childTotal = 0;
float adultTotal = 0;
float seniorTotal = 0;
float finalTotal = 0; //minor change
final double childCost = 18;
final double adultCost = 36;
final double seniorCost = 32.50;
boolean continueLoop = true;
char resume;
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:
finalTotal+=(double) quantity*childTotal ; //minor change
System.out.println("Total amount payable: $" + childTotal);
break;
case 2:
finalTotal+=(double) quantity*adultTotal ; //minor change
System.out.println("Total amount payable $" + adultTotal);
break;
default:
finalTotal+=(double) quantity*seniorTotal; //minor change
System.out.println("Total amount payable $" + seniorTotal);
break;
}
}
}
}
}
I wished to play and did not fully understand the problem...so i developed from scratch the application. Suryakant was faster so please accept his answer (if it solves your problem). I simply post this here since i worked on it :-)
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
boolean continueLoop = true;
Map<TicketType, Integer> purchases=new HashMap<>();
do {
TicketType type = printMenu(scan);
System.out.println("Enter number of tickets for " + type.label);
int quantity = scan.nextInt();
System.out.println("You are purchasing "+quantity + " "+ type.label+ " ticket at "+type.cost+" each. " +
"Press 1 to confirm?");
int confirm= scan.nextInt();
if (confirm!=1) continue;
if (purchases.containsKey(type)){
purchases.put(type,purchases.get(type)+quantity);
System.out.println("You now have " +purchases.get(type) +" "+type.label +" tickets in total");
}else {
purchases.put(type,quantity);
}
System.out.println("You have added " +quantity +" "+type.label +" tickets in your basket.");
System.out.println("Do you wish to continue (Y|N)?");
String resume=scan.next();
if (resume.startsWith("Y") || resume.startsWith("y")){
continueLoop=true;
}else {
continueLoop=false;
}
}while (continueLoop);
System.out.println("Purchases");
long total=0;
for (Map.Entry<TicketType, Integer> ticketTypeIntegerEntry : purchases.entrySet()) {
System.out.println(ticketTypeIntegerEntry.getKey().label+"("+ticketTypeIntegerEntry.getValue()+")");
total+=ticketTypeIntegerEntry.getKey().cost*ticketTypeIntegerEntry.getValue();
}
System.out.println("Total payable ammount: "+total);
}
private static TicketType printMenu(Scanner scan) {
System.out.println("Welcome");
TicketType type;
int k = -1;
do {
for (TicketType ticketType : TicketType.values()) {
System.out.println(ticketType.id + ". for " + ticketType.label);
}
System.out.println("Enter your option");
k = scan.nextInt();
} while ((type=TicketType.valuefromId(k))==null);
return type;
}
private enum TicketType {
CHILD(1, "Child", 18D),
ADULT(2, "Adult", 36D),
SENIOR(3, "Senior", 18.5D);
int id;
String label;
double cost;
private static Map<Integer,TicketType> map=new HashMap<Integer,TicketType>();
static {
for (TicketType ticketType : TicketType.values()) {
map.put(ticketType.id,ticketType);
}
}
TicketType(int id, String label, double cost) {
this.id = id;
this.label = label;
this.cost=cost;
}
public static TicketType valuefromId(int id){
return map.get(id);
}
}
}
improvements are in reading.. i would check first if what i read is character or not..
Ok guys so I'm stuck trying to write some code in java, I cant get the code to display the pricing option for fullsize. I can't get the program to continue onto the second option I have listed as Case 2.
The project basically gives the user the option to ask if he is renting a car [Y or N]:
if Y is inputed the next question
it ask is "Compact of Full-size?",
if the user selects compact the project displays that the user has selected compact and
if the code displays fullsize the project displays that the user has selected fullsize.
Then it asks the user if they have a coupon if the users answer Y for the coupon the price is 7% off of 30.50.
If the user answers N the price is a normal 30.50. The fullsize normal price is 40.50 and the price with a coupon is 7% off of 40.50. The following is the code i have written currently.
The code:
public class CarRental {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
System.out.println("Programmed by .");
double standardCompact = 30.50;
double couponCompact = ((30.50) - (30.50 * 0.07));
double standardFullSize = 40.50;
double couponFullSize = ((40.50) - (40.50 * 0.07));
//Scanner Input
Scanner input = new Scanner(System.in);
System.out.print("Rent a Car? [Y or N]: ");
//Response String
String response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("You entered no. Bye. ");
} else if (response.equals("Y")) {
System.out.print("Compact or Full-Size? [C or F]: ");
//case1
response = input.next().toUpperCase();
if (response.equals("C")) {
System.out.println("You selected Compact. ");
} else if (response.equals("F")) {
System.out.println("You have selected Full-Size. ");
System.out.print("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardCompact + " " + "per day.");
} else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponCompact + " " + "per day.");
//case 2
response = input.next().toUpperCase();
if (response.equals("F")) {
System.out.println("You have selected Full-Size.");
System.out.println("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardFullSize + " " + "per day.");
} else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponFullSize + " " + "per day.");
}
}
}
You're missing some }s after your else clauses. Example:
response = input.next().toUpperCase();
if (response.equals("C")) {
System.out.println("You selected Compact. ");
//Put code that should only execute if you select Compact here.
}else if(response.equals("F")){
System.out.println("You have selected Full-Size. ");
//Put code that should only execute if you select Full-size here.
//Should have a } here!
//Put code that should always execute here.
Because you never close the block of code in the else clause, all of the code that follows is still part of the else, and therefore will only be executed if the else is selected, not under every circumstance as you had intended.
You are opening lots of brackets { but not closing them where you need }.
I usually not just handing the code, but I've noticed you done must of the job, but confused where to close the brackets and a little bit at the program flow.
I only changed it a bit, there is a lot that you can cut and reuse code.
public static void main(String[] args){
for(int i = 0; i < 4; i++) {
System.out.println("Programmed by .");
double standardCompact = 30.50;
double couponCompact = ((30.50)-(30.50 * 0.07));
double standardFullSize = 40.50;
double couponFullSize = ((40.50)-(40.50 * 0.07));
//Scanner Input
Scanner input = new Scanner(System.in);
System.out.print("Rent a Car? [Y or N]: ");
//Response String
String response = input.next().toUpperCase();
if (response.equals("N")){
System.out.println("You entered no. Bye. ");
}
else if (response.equals("Y")) {
System.out.print("Compact or Full-Size? [C or F]: ");
response = input.next().toUpperCase();
if (response.equals("C")) {
System.out.println("You selected Compact. ");
//case1
System.out.print("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardCompact + " " + "per day.");
}
else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponCompact + " " + "per day.");
}
}
else if(response.equals("F")) {
System.out.println("You have selected Full-Size. ");
//case 2
System.out.print("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardFullSize + " " + "per day.");
}
else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponFullSize + " " + "per day.");
}
}
}
}
}
The problem with this code is all your code is under the if statemente for Full-Size so the case 2 only executes when you select full-size , Yes to coupon and after showing the final message you press F again the code should look like this.
public class CarRental {
public static void main(String[] args){
for(int i=0; i<4; i++){
System.out.println("Programmed by .");
double standardCompact = 30.50;
double couponCompact = ((30.50)-(30.50 * 0.07));
double standardFullSize = 40.50;
double couponFullSize = ((40.50)-(40.50 * 0.07));
//Scanner Input
Scanner input = new Scanner(System.in);
System.out.print("Rent a Car? [Y or N]: ");
//Response String
String response = input.next().toUpperCase();
if (response.equals("N")){
System.out.println("You entered no. Bye. ");
}else if (response.equals("Y")) {
System.out.print("Compact or Full-Size? [C or F]: ");
//case1
response = input.next().toUpperCase();
if (response.equals("C")) {
System.out.println("You selected Compact. ");
System.out.print("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardCompact + " " + "per day.");
} else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponCompact + " " + "per day.");
}
//case 2
}else if(response.equals("F")){
System.out.println("You have selected Full-Size. ");
System.out.println("Have coupon? [Y or N]: ");
response = input.next().toUpperCase();
if (response.equals("N")) {
System.out.println("Price is" + " " + standardFullSize + " " + "per day.");
} else if (response.equals("Y")) {
System.out.println("Price is" + " " + couponFullSize + " " + "per day.");
}
}
}
}
}
As you can see in the code above is really important to correctly close conditional blocks so the code really does what you expect it to do.
Using flow diagrams is a good support for learning how programming languages really work.
I have a POS program that is not working it says switch(food) unreachable statement
import javax.swing.*;
import java.awt.*;
public class sari{
public static void One()
{
int choice=0,food=0,c=0,con,x=1,y=1,z=1,q=1,trans=0,price=0 ,qty,gtotal=0,ptotal=0,pay,change,total=0,ord;
String order="",bibilhin="",transaction="",A;
ImageIcon welcome = new ImageIcon("welcome.jpg");
ImageIcon chip = new ImageIcon("chip.jpg");
ImageIcon rc = new ImageIcon("rc.jpg");
ImageIcon stick = new ImageIcon("stick.jpg");
ImageIcon pancit = new ImageIcon("pancit.jpg");
ImageIcon jampong = new ImageIcon("jampong.jpg");
ImageIcon chups = new ImageIcon("chups.jpg");
ImageIcon egg = new ImageIcon("egg.jpg");
A=JOptionPane.showInputDialog("Enter your name:"); ImageIcon hansel = new ImageIcon("hansel.jpg");
JOptionPane.showMessageDialog(null,"Sir/Ma`am \n "+A+"\n Welcome\n to Mang Inasal\n Please Choose The\n product you want to buy","Welcome" ,JOptionPane.PLAIN_MESSAGE,welcome);
while(x==1)
{
c=Integer.parseInt(JOptionPane.showInputDialog(" Categories" + "\n" +"[1] Drinks" + "\n" + "[2] Foods"));
if(c==1)
{choice=Integer.parseInt(JOptionPane.showInputDialog("DRINKS" +"\n"
+ "[1] Lemonade" + "\n"
+ "[2] Coca Cola" + "\n"
+ "[3] Sprite" + "\n"
+ "[4] Mountain Dew" + "\n"
+ "[5] Pepsi" + "\n"
+ "[6] Cofee" +"\n"
+ "[7] Hot Choco" + "\n"
+ "[8] Nestie Iced Tea" +"\n"
+ "[9] Exit" +"\n"
+ "Enter Your Choice:"));
if(c==2)
food=Integer.parseInt(JOptionPane.showInputDialog("Food" +"\n"
+ "[1] L" + "\n"
+ "[2] Coca Cola" + "\n"
+ "[3] Sprite" + "\n"
+ "[4] Mountain Dew" + "\n"
+ "[5] Pepsi" + "\n"
+ "[6] Cofee" +"\n"
+ "[7] Hot Choco" + "\n"
+ "[8] Nestie Iced Tea" +"\n"
+ "[9] Exit" +"\n"
+ "Enter Your Choice:"));
}
switch(choice)
{
case 1:
price = 20;
bibilhin= " Lemonade";
break;
case 2:
price = 20;
bibilhin= "Coca Cola ";
break;
case 3:
price =820;
bibilhin = "Sprite";
break;
case 4:
price =205;
bibilhin= "Mountain dew";
break;
case 5:
price =20;
bibilhin= "Pepsi";
break;
case 6:
price =25;
bibilhin= "Cofee ";
break;
case 7:
price =25;
bibilhin= "Hot Choco";
break;
case 8:
price = 20;
bibilhin= "Nestie Ice tea";
break;
case 9:
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"You are about to exit!","Exit",JOptionPane.WARNING_MESSAGE);
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Are you sure you want to exit?\n[1] Yes\n[0] No"));
if(con==1)
System.exit(0);
else if(con==1)
{
JOptionPane.showMessageDialog(null,"THANK YOU!\nGood Bye!","Exit",JOptionPane.ERROR_MESSAGE);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
con=1;
}
}while(con==1);
continue;
default:
JOptionPane.showMessageDialog(null,"Invalid Choice!","ERROR",JOptionPane.ERROR_MESSAGE);
}
do{//do1
qty = Integer.parseInt(JOptionPane.showInputDialog("Quantity:"));
if (qty>0)
break;
else
{
JOptionPane.showMessageDialog(null,"Invalid Input!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(q==1);//end do1
total = price * qty;
ptotal = ptotal+total;
order = order +bibilhin+" "+qty+" "+price+"="+total+"\n";
do {//do2
con = Integer.parseInt(JOptionPane.showInputDialog("Continue?[1] yes [0] no"));
if(con==1)
{
break;
}
else if(con==0)
{
do{
pay = Integer.parseInt(JOptionPane.showInputDialog(order+"Total " +ptotal+"\nEnter Payment:"));
if(pay>=ptotal)
{
change = pay-ptotal;
JOptionPane.showMessageDialog(null," \t Mang Inasal"+
"\n\t 14 T molina st purok 6-B "+
"\n\t Alabang Muntinlupa City "+
"\nOperator: Micko Mendoza"+
"\n\t----------------------------------------------------------------"+
"\n\n\t "+order+"\nTotal "+ptotal+
"\nCash "+pay+
"\nChange "+change+
"\n----------------------------------------------------------------"+
"\n\n \t Thanks For buying!!!!!"+
"\n\t This Serve as an official reciept DTSN:41D983"+
"\n \t For Delivery Dial (519-6936)"+
"\n \t Feedbacks"+
"\n \t micko.mendoza#yahoo.com"
);
y=0;
z=0;
x=1;
gtotal = gtotal+ptotal;
trans++;
order="";
transaction = transaction+"Transaction "+ trans+ " "+ptotal+"\n";//No of transactions
ptotal = 0;
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Next Customer?\n[1] Yes\n[0] No"));
if(con==1)
break;
else if(con==0)
{
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(con==1);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Kulang pera mo");
continue;
}
}while(z==1);//do3
}
else
{
JOptionPane.showMessageDialog(null,"Intiger lang pwede","Continue",JOptionPane.ERROR_MESSAGE);
y=1;
continue;
}
} while(y==1);//do2
break;
switch(food)
{
case 11:
price = 20;
bibilhin= " Lemonade";
break;
case 12:
price = 20;
bibilhin= "Coca Cola ";
break;
case 13:
price =820;
bibilhin = "Sprite";
break;
case 14:
price =205;
bibilhin= "Mountain dew";
break;
case 15:
price =20;
bibilhin= "Pepsi";
break;
case 16:
price =25;
bibilhin= "Cofee ";
break;
case 17:
price =25;
bibilhin= "Hot Choco";
break;
case 18:
price = 20;
bibilhin= "Nestie Ice tea";
break;
case 19:
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"You are about to exit!","Exit",JOptionPane.WARNING_MESSAGE);
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Are you sure you want to exit?\n[1] Yes\n[0] No"));
if(con==1)
System.exit(0);
else if(con==1)
{
JOptionPane.showMessageDialog(null,"THANK YOU!\nGood Bye!","Exit",JOptionPane.ERROR_MESSAGE);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
con=1;
}
}while(con==1);
continue;
default:
JOptionPane.showMessageDialog(null,"Invalid Choice!","ERROR",JOptionPane.ERROR_MESSAGE);
}
do{//do1
qty = Integer.parseInt(JOptionPane.showInputDialog("Quantity:"));
if (qty>0)
break;
else
{
JOptionPane.showMessageDialog(null,"Invalid Input!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(q==1);//end do1
total = price * qty;
ptotal = ptotal+total;
order = order +bibilhin+" "+qty+" "+price+"="+total+"\n";
do {//do2
con = Integer.parseInt(JOptionPane.showInputDialog("Continue?[1] yes [0] no"));
if(con==1)
{
break;
}
else if(con==0)
{
do{
pay = Integer.parseInt(JOptionPane.showInputDialog(order+"Total " +ptotal+"\nEnter Payment:"));
if(pay>=ptotal)
{
change = pay-ptotal;
JOptionPane.showMessageDialog(null," \t Mang Inasal"+
"\n\t 14 T molina st purok 6-B "+
"\n\t Alabang Muntinlupa City "+
"\nOperator: Micko Mendoza"+
"\n\t----------------------------------------------------------------"+
"\n\n\t "+order+"\nTotal "+ptotal+
"\nCash "+pay+
"\nChange "+change+
"\n----------------------------------------------------------------"+
"\n\n \t Thanks For buying!!!!!"+
"\n\t This Serve as an official reciept DTSN:41D983"+
"\n \t For Delivery Dial (519-6936)"+
"\n \t Feedbacks"+
"\n \t micko.mendoza#yahoo.com"
);
y=0;
z=0;
x=1;
gtotal = gtotal+ptotal;
trans++;
order="";
transaction = transaction+"Transaction "+ trans+ " "+ptotal+"\n";//No of transactions
ptotal = 0;
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Next Customer?\n[1] Yes\n[0] No"));
if(con==1)
break;
else if(con==0)
{
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(con==1);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Kulang pera mo");
continue;
}
}while(z==1);//do3
}
else
{
JOptionPane.showMessageDialog(null,"Intiger lang pwede","Continue",JOptionPane.ERROR_MESSAGE);
y=1;
continue;
}
} while(y==1);//do2
continue;
}
}
}
You have an unconditional break right before the switch in question.
As an aside, I think this is the perfect time to learn about functions and how to use them to make code more modular.
comment the break statement
import javax.swing.;
import java.awt.;
public class Test{
public static void main(String[] args)
{
int choice=0,food=0,c=0,con,x=1,y=1,z=1,q=1,trans=0,price=0 ,qty,gtotal=0,ptotal=0,pay,change,total=0,ord;
String order="",bibilhin="",transaction="",A;
ImageIcon welcome = new ImageIcon("welcome.jpg");
ImageIcon chip = new ImageIcon("chip.jpg");
ImageIcon rc = new ImageIcon("rc.jpg");
ImageIcon stick = new ImageIcon("stick.jpg");
ImageIcon pancit = new ImageIcon("pancit.jpg");
ImageIcon jampong = new ImageIcon("jampong.jpg");
ImageIcon chups = new ImageIcon("chups.jpg");
ImageIcon egg = new ImageIcon("egg.jpg");
A=JOptionPane.showInputDialog("Enter your name:"); ImageIcon hansel = new ImageIcon("hansel.jpg");
JOptionPane.showMessageDialog(null,"Sir/Ma`am \n "+A+"\n Welcome\n to Mang Inasal\n Please Choose The\n product you want to buy","Welcome" ,JOptionPane.PLAIN_MESSAGE,welcome);
while(x==1)
{
c=Integer.parseInt(JOptionPane.showInputDialog(" Categories" + "\n" +"[1] Drinks" + "\n" + "[2] Foods"));
if(c==1)
{choice=Integer.parseInt(JOptionPane.showInputDialog("DRINKS" +"\n"
+ "[1] Lemonade" + "\n"
+ "[2] Coca Cola" + "\n"
+ "[3] Sprite" + "\n"
+ "[4] Mountain Dew" + "\n"
+ "[5] Pepsi" + "\n"
+ "[6] Cofee" +"\n"
+ "[7] Hot Choco" + "\n"
+ "[8] Nestie Iced Tea" +"\n"
+ "[9] Exit" +"\n"
+ "Enter Your Choice:"));
if(c==2)
food=Integer.parseInt(JOptionPane.showInputDialog("Food" +"\n"
+ "[1] L" + "\n"
+ "[2] Coca Cola" + "\n"
+ "[3] Sprite" + "\n"
+ "[4] Mountain Dew" + "\n"
+ "[5] Pepsi" + "\n"
+ "[6] Cofee" +"\n"
+ "[7] Hot Choco" + "\n"
+ "[8] Nestie Iced Tea" +"\n"
+ "[9] Exit" +"\n"
+ "Enter Your Choice:"));
}
switch(choice)
{
case 1:
price = 20;
bibilhin= " Lemonade";
break;
case 2:
price = 20;
bibilhin= "Coca Cola ";
break;
case 3:
price =820;
bibilhin = "Sprite";
break;
case 4:
price =205;
bibilhin= "Mountain dew";
break;
case 5:
price =20;
bibilhin= "Pepsi";
break;
case 6:
price =25;
bibilhin= "Cofee ";
break;
case 7:
price =25;
bibilhin= "Hot Choco";
break;
case 8:
price = 20;
bibilhin= "Nestie Ice tea";
break;
case 9:
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"You are about to exit!","Exit",JOptionPane.WARNING_MESSAGE);
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Are you sure you want to exit?\n[1] Yes\n[0] No"));
if(con==1)
System.exit(0);
else if(con==1)
{
JOptionPane.showMessageDialog(null,"THANK YOU!\nGood Bye!","Exit",JOptionPane.ERROR_MESSAGE);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
con=1;
}
}while(con==1);
continue;
default:
JOptionPane.showMessageDialog(null,"Invalid Choice!","ERROR",JOptionPane.ERROR_MESSAGE);
}
do{//do1
qty = Integer.parseInt(JOptionPane.showInputDialog("Quantity:"));
if (qty>0)
break;
else
{
JOptionPane.showMessageDialog(null,"Invalid Input!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(q==1);//end do1
total = price * qty;
ptotal = ptotal+total;
order = order +bibilhin+" "+qty+" "+price+"="+total+"\n";
do {//do2
con = Integer.parseInt(JOptionPane.showInputDialog("Continue?[1] yes [0] no"));
if(con==1)
{
break;
}
else if(con==0)
{
do{
pay = Integer.parseInt(JOptionPane.showInputDialog(order+"Total " +ptotal+"\nEnter Payment:"));
if(pay>=ptotal)
{
change = pay-ptotal;
JOptionPane.showMessageDialog(null," \t Mang Inasal"+
"\n\t 14 T molina st purok 6-B "+
"\n\t Alabang Muntinlupa City "+
"\nOperator: Micko Mendoza"+
"\n\t----------------------------------------------------------------"+
"\n\n\t "+order+"\nTotal "+ptotal+
"\nCash "+pay+
"\nChange "+change+
"\n----------------------------------------------------------------"+
"\n\n \t Thanks For buying!!!!!"+
"\n\t This Serve as an official reciept DTSN:41D983"+
"\n \t For Delivery Dial (519-6936)"+
"\n \t Feedbacks"+
"\n \t micko.mendoza#yahoo.com"
);
y=0;
z=0;
x=1;
gtotal = gtotal+ptotal;
trans++;
order="";
transaction = transaction+"Transaction "+ trans+ " "+ptotal+"\n";//No of transactions
ptotal = 0;
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Next Customer?\n[1] Yes\n[0] No"));
if(con==1)
break;
else if(con==0)
{
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(con==1);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Kulang pera mo");
continue;
}
}while(z==1);//do3
}
else
{
JOptionPane.showMessageDialog(null,"Intiger lang pwede","Continue",JOptionPane.ERROR_MESSAGE);
y=1;
continue;
}
} while(y==1);//do2
// break;
switch(food)
{
case 11:
price = 20;
bibilhin= " Lemonade";
break;
case 12:
price = 20;
bibilhin= "Coca Cola ";
break;
case 13:
price =820;
bibilhin = "Sprite";
break;
case 14:
price =205;
bibilhin= "Mountain dew";
break;
case 15:
price =20;
bibilhin= "Pepsi";
break;
case 16:
price =25;
bibilhin= "Cofee ";
break;
case 17:
price =25;
bibilhin= "Hot Choco";
break;
case 18:
price = 20;
bibilhin= "Nestie Ice tea";
break;
case 19:
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,"You are about to exit!","Exit",JOptionPane.WARNING_MESSAGE);
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Are you sure you want to exit?\n[1] Yes\n[0] No"));
if(con==1)
System.exit(0);
else if(con==1)
{
JOptionPane.showMessageDialog(null,"THANK YOU!\nGood Bye!","Exit",JOptionPane.ERROR_MESSAGE);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
con=1;
}
}while(con==1);
continue;
default:
JOptionPane.showMessageDialog(null,"Invalid Choice!","ERROR",JOptionPane.ERROR_MESSAGE);
}
do{//do1
qty = Integer.parseInt(JOptionPane.showInputDialog("Quantity:"));
if (qty>0)
break;
else
{
JOptionPane.showMessageDialog(null,"Invalid Input!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(q==1);//end do1
total = price * qty;
ptotal = ptotal+total;
order = order +bibilhin+" "+qty+" "+price+"="+total+"\n";
do {//do2
con = Integer.parseInt(JOptionPane.showInputDialog("Continue?[1] yes [0] no"));
if(con==1)
{
break;
}
else if(con==0)
{
do{
pay = Integer.parseInt(JOptionPane.showInputDialog(order+"Total " +ptotal+"\nEnter Payment:"));
if(pay>=ptotal)
{
change = pay-ptotal;
JOptionPane.showMessageDialog(null," \t Mang Inasal"+
"\n\t 14 T molina st purok 6-B "+
"\n\t Alabang Muntinlupa City "+
"\nOperator: Micko Mendoza"+
"\n\t----------------------------------------------------------------"+
"\n\n\t "+order+"\nTotal "+ptotal+
"\nCash "+pay+
"\nChange "+change+
"\n----------------------------------------------------------------"+
"\n\n \t Thanks For buying!!!!!"+
"\n\t This Serve as an official reciept DTSN:41D983"+
"\n \t For Delivery Dial (519-6936)"+
"\n \t Feedbacks"+
"\n \t micko.mendoza#yahoo.com"
);
y=0;
z=0;
x=1;
gtotal = gtotal+ptotal;
trans++;
order="";
transaction = transaction+"Transaction "+ trans+ " "+ptotal+"\n";//No of transactions
ptotal = 0;
do{
con=Integer.parseInt(JOptionPane.showInputDialog("Next Customer?\n[1] Yes\n[0] No"));
if(con==1)
break;
else if(con==0)
{
JOptionPane.showMessageDialog(null,transaction+"Total sales: " + gtotal,"Transactions",JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Choice!","Error",JOptionPane.ERROR_MESSAGE);
continue;
}
}while(con==1);
break;
}
else
{
JOptionPane.showMessageDialog(null,"Kulang pera mo");
continue;
}
}while(z==1);//do3
}
else
{
JOptionPane.showMessageDialog(null,"Intiger lang pwede","Continue",JOptionPane.ERROR_MESSAGE);
y=1;
continue;
}
} while(y==1);//do2
continue;
}
}
}