I'm writing a party planner program for a question for class.
I can't initialize my three choices of entertainment, decorations, and food.
Eclipse tells me that I should set all these values to 0. I have to stick to these if else statements because that's what we have learned so far.
Here is my code:
import java.util.Scanner;
public class PartyPlanner {
public static void main(String[] args) {
// TODO Auto-generated method stub
int entertainment;
int decorations;
int food;
int budget = entertainment + decorations + food;
Scanner keyboard = new Scanner(System.in);
System.out.println("For your choices, please type"
+ " in what is contained in the brackets."
+ " We will do the calculations for you.");
System.out.println("Choose entertainment:"
+ " [band] for $400 or " + "[DJ] for $150");
String choice1 = keyboard.nextLine();
if (choice1 == "DJ")
{
entertainment = 400;
}
else if (choice1 == "band")
{
entertainment = 150;
}
System.out.println("Where would you like to buy "
+ "decorations? [school] for $100 or [your own] for $250 ?");
String choice2 = keyboard.nextLine();
if (choice2 == "school")
{
decorations = 100;
}
else if (choice2 == "your own")
{
decorations = 250;
}
System.out.println("Would you like to purchase"
+ " [pizza] for $200 or [sub sandwiches]"
+ " for $250 or [appetizers] for $150?");
String choice3 = keyboard.nextLine();
if (choice3 == "pizza")
{
food = 200;
}
else if (choice3 == "sub sandwiches")
{
food = 250;
}
else if (choice3 == "appetizers")
{
food = 150;
}
System.out.println("You have chosen: " + choice1 +
" and " + choice2 + " and " + choice3);
System.out.println("The total cost of this party"
+ " comes out to:" + budget);
}
}
The problem is
The local variable entertainment, decorations, and food may have not been initalized.
First , you have to make the sum at the end of the code because at first the variables are not initialized yet , and secondly you can assign a value of zero if no choice have been made , like this :
public class PartyPlanner {
public static void main(String[] args) {
// TODO Auto-generated method stub
int entertainment;
int decorations;
int food;
Scanner keyboard = new Scanner(System.in);
System.out.println("For your choices, please type"
+ " in what is contained in the brackets."
+ " We will do the calculations for you.");
System.out.println("Choose entertainment:"
+ " [band] for $400 or " + "[DJ] for $150");
String choice1 = keyboard.nextLine();
if (choice1 == "DJ")
{
entertainment = 400;
}
else if (choice1 == "band")
{
entertainment = 150;
}
else { entertainment = 0; }
System.out.println("Where would you like to buy "
+ "decorations? [school] for $100 or [your own] for $250 ?");
String choice2 = keyboard.nextLine();
if (choice2 == "school")
{
decorations = 100;
}
else if (choice2 == "your own")
{
decorations = 250;
}
else { decorations = 0; }
System.out.println("Would you like to purchase"
+ " [pizza] for $200 or [sub sandwiches]"
+ " for $250 or [appetizers] for $150?");
String choice3 = keyboard.nextLine();
if (choice3 == "pizza")
{
food = 200;
}
else if (choice3 == "sub sandwiches")
{
food = 250;
}
else if (choice3 == "appetizers")
{
food = 150;
}
else { food = 0 ; }
int budget = entertainment + decorations + food;
System.out.println("You have chosen: " + choice1 +
" and " + choice2 + " and " + choice3);
System.out.println("The total cost of this party"
+ " comes out to:" + budget);
}
}
i have a cashiering system and here's my codes:
import javax.swing.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FinalProject {
private static final int totalsundae = 0;
public static void main(String[] args) {
int totaldrinks = 0, totalfries = 0, totalburger = 0, totalsundae = 0;
FinalProject m = new FinalProject();
m.Cashiering();
}
public void Cashiering() {
String menu[] = {"Customer's Name", "Menu", "Pay", "View Order list", "Exit"};
String choice = "";
char ch;
do {
choice = (String) JOptionPane.showInputDialog(null, "Please select your order", "menu", 1, null, menu, menu[0]);
ch = choice.toLowerCase().charAt(0);
switch (ch) {
case 'c':
customer();
break;
case 'm':
menu2();
break;
case 'p':
getTotal();
break;
case 'v':
orderList();
break;
case 'e':
System.exit(0);
}
} while (!choice.equals("Close"));
}
public void customer() {
String name = (String) JOptionPane.showInputDialog("WELCOME !!!!! \n\nPLEASE ENTER YOUR NAME: ");
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
printW.println("Customer's Name " + "\n" + name);
JOptionPane.showMessageDialog(null, "Your Name successfully stored \n\n You can Order Now ");
printW.close();
} catch (Exception e) {
}
}
public int burger() {
int burger = 0, quantity = 0, totalburger = 0;
int choice;
String burgnm = "";
String menu = "\nBURGER: \n1.Regular Burger 45 \n2.Burger with cheese---->55 \n3.Burger with egg and cheese----50 \n4.Burger with ham---->60 \n5.Burger with ham and cheese---->70 ";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
burger += 45;
burgnm = "Regular Burger - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
burger += 55;
burgnm = "Burger with cheese - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 3:
burger += 50;
burgnm = "Burger with egg and cheese - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 4:
burger += 60;
burgnm = "Burger with ham - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 5:
burger += 70;
burgnm = "Burger with ham and cheese - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalburger = quantity * burger;
burgnm = burgnm + totalburger + "\n";
JOptionPane.showMessageDialog(null, "Total = " + burgnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity + burgnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity + burgnm);
else if (choice == 3)
printW.println("Customer's Order " + "\n" + quantity + burgnm);
else if (choice == 4)
printW.println("Customer's Order " + "\n" + quantity + burgnm);
else if (choice == 5)
printW.println("Customer's Order " + "\n" + quantity + burgnm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totalburger;
}
public int Spaghetti() {
int choice, quantity = 0, totalspag = 0, spaghetti = 0;
String spagnm = null;
String menu = "\nSPAGHETTI: \n1.Regular spaghetti---->60 \n2.Large Spaghetti---->70";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
spaghetti += 60;
spagnm = "Regular Spaghetti - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
spaghetti += 70;
spagnm = "Large Spaghetti - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalspag = quantity * spaghetti;
spagnm = spagnm + totalspag + "\n";
JOptionPane.showMessageDialog(null, "Total = " + spagnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity + spagnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity + spagnm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totalspag;
}
public int Fries() {
int choice, fries = 0, quantity = 0, totalfries = 0;
String friesnm = "";
String menu = "\nFRIES: \n1.Regular Fries ----> 35\n2.Medium Fries ----> 45 \n3.LargeFries ----> 55";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
fries += 35;
friesnm = "Regular Fries - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
fries += 45;
friesnm = "Medium Fries - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 3:
fries += 55;
friesnm = "Large Fries - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalfries = quantity * fries;
friesnm = friesnm + totalfries + "\n";
JOptionPane.showMessageDialog(null, "Total = " + friesnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity + friesnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity + friesnm);
else if (choice == 3)
printW.println("Customer's Order " + "\n" + quantity + friesnm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totalfries;
}
public int Sundae() {
int choice, quantity = 0, sundae = 0, totalsun = 0;
String sundaenm = "";
String menu = "\nSUNDAE: \n1.Choco sundae ----> 28 \n2.Caramel sundae ----> 28 \n3.Strawberry sundae ----> 28";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
sundae += 28;
sundaenm = " Choco Sundae -";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
sundae += 28;
sundaenm = " Caramel Sundae ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 3:
sundae += 28;
sundaenm = " Strawberry Sundae - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalsun = quantity * sundae;
sundaenm = sundaenm + totalsun + "\n";
JOptionPane.showMessageDialog(null, "Total = " + sundaenm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Costumer's Order " + "\n" + quantity + sundaenm);
} else if (choice == 2)
printW.println("Costumer's Order " + "\n" + quantity + sundaenm);
else if (choice == 3)
printW.println("Costumer's Order " + "\n" + quantity + sundaenm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totalsun;
}
public int Drinks() {
int choice, quantity = 0, drinks = 0, totaldrinks = 0;
String drinksnm = "";
String menu = "\nDRINKS: \n1.Reg. Coke ----> 25 \n2.Large Coke ----> 35 \n3.Reg.Sprite ----> 28 \n4.Large Sprite ----> 38";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: " + menu));
switch (choice) {
case 1:
drinks += 25;
drinksnm = "Regular Coke - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 2:
drinks += 35;
drinksnm = "Large Coke - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 3:
drinks += 28;
drinksnm = "Regular Sprite - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
case 4:
drinksnm = "Large Sprite - ";
quantity = Integer.parseInt(JOptionPane.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totaldrinks = quantity * drinks;
drinksnm = drinksnm + totaldrinks + "\n";
JOptionPane.showMessageDialog(null, "Total = " + drinksnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt", true));
if (choice == 1) {
printW.println("Costumer's Order " + "\n" + quantity + drinksnm);
} else if (choice == 2)
printW.println("Costumer's Order " + "\n" + quantity + drinksnm);
else if (choice == 3)
printW.println("Costumer's Order " + "\n" + quantity + drinksnm);
else if (choice == 4)
printW.println("Costumer's Order " + "\n" + quantity + drinksnm);
JOptionPane.showMessageDialog(null, "Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
return totaldrinks;
}
public void orderList() {
try {
Scanner in = new Scanner(new FileReader("cash.txt"));
String all = "\nWELCOME !!!! \nCASHIERING SYSTEM\n";
while (in.hasNext()) {
all = all + in.nextLine() + "\n";
}
JOptionPane.showMessageDialog(null, new JTextArea(all));
in.close();
} catch (Exception e) {
}
}
public void getTotal() {
}
public void menu2() {
String menu[] = {"Burger", "Spaghetti", "Fries", "Ice Sundae", "Drinks", "Exit"};
String choice = "";
char ch;
int yesorno = 0;
do {
choice = (String) JOptionPane.showInputDialog(null, "Please select your order", "menu", 1, null, menu, menu[0]);
ch = choice.toLowerCase().charAt(0);
switch (ch) {
case 'b':
burger();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 's':
Spaghetti();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'f':
Fries();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'i':
Sundae();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'd':
Drinks();
yesorno = Integer.parseInt(JOptionPane.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'e':
break;
}
} while (!choice.equals("Close"));
}
}
my problem here is the total. how can i get the total of the customer's order by using a method? i'm really a beginner in java so i hope you can give some simple codes. thank you so much for your help!
Use a totalOrder variable to store the total for that session.
import javax.swing.*;
import java.util.Scanner;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class FinalProject {
private int totalOrder;
private static final int totalsundae = 0;
public static void main(String[] args) {
int totaldrinks = 0, totalfries = 0, totalburger = 0, totalsundae = 0;
FinalProject m = new FinalProject();
m.Cashiering();
}
public void Cashiering() {
String menu[] = { "Customer's Name", "Menu", "Pay", "View Order list",
"Exit" };
String choice = "";
char ch;
do {
choice = (String) JOptionPane.showInputDialog(null,
"Please select your order", "menu", 1, null, menu, menu[0]);
ch = choice.toLowerCase().charAt(0);
switch (ch) {
case 'c':
customer();
break;
case 'm':
menu2();
break;
case 'p':
getTotal();
break;
case 'v':
orderList();
break;
case 'e':
System.exit(0);
}
} while (!choice.equals("Close"));
}
public void customer() {
String name = (String) JOptionPane
.showInputDialog("WELCOME !!!!! \n\nPLEASE ENTER YOUR NAME: ");
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
printW.println("Customer's Name " + "\n" + name);
JOptionPane.showMessageDialog(null,
"Your Name successfully stored \n\n You can Order Now ");
printW.close();
} catch (Exception e) {
}
}
public int burger() {
int burger = 0, quantity = 0, totalburger = 0;
int choice;
String burgnm = "";
String menu = "\nBURGER: \n1.Regular Burger 45 \n2.Burger with cheese---->55 \n3.Burger with egg and cheese----50 \n4.Burger with ham---->60 \n5.Burger with ham and cheese---->70 ";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
burger += 45;
burgnm = "Regular Burger - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
burger += 55;
burgnm = "Burger with cheese - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 3:
burger += 50;
burgnm = "Burger with egg and cheese - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 4:
burger += 60;
burgnm = "Burger with ham - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 5:
burger += 70;
burgnm = "Burger with ham and cheese - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalburger = quantity * burger;
burgnm = burgnm + totalburger + "\n";
JOptionPane.showMessageDialog(null, "Total = " + burgnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
else if (choice == 3)
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
else if (choice == 4)
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
else if (choice == 5)
printW.println("Customer's Order " + "\n" + quantity
+ burgnm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totalburger;
return totalburger;
}
public int Spaghetti() {
int choice, quantity = 0, totalspag = 0, spaghetti = 0;
String spagnm = null;
String menu = "\nSPAGHETTI: \n1.Regular spaghetti---->60 \n2.Large Spaghetti---->70";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
spaghetti += 60;
spagnm = "Regular Spaghetti - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
spaghetti += 70;
spagnm = "Large Spaghetti - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalspag = quantity * spaghetti;
spagnm = spagnm + totalspag + "\n";
JOptionPane.showMessageDialog(null, "Total = " + spagnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity
+ spagnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity
+ spagnm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totalspag;
return totalspag;
}
public int Fries() {
int choice, fries = 0, quantity = 0, totalfries = 0;
String friesnm = "";
String menu = "\nFRIES: \n1.Regular Fries ----> 35\n2.Medium Fries ----> 45 \n3.LargeFries ----> 55";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
fries += 35;
friesnm = "Regular Fries - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
fries += 45;
friesnm = "Medium Fries - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 3:
fries += 55;
friesnm = "Large Fries - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalfries = quantity * fries;
friesnm = friesnm + totalfries + "\n";
JOptionPane.showMessageDialog(null, "Total = " + friesnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Customer's Order " + "\n" + quantity
+ friesnm);
} else if (choice == 2)
printW.println("Customer's Order " + "\n" + quantity
+ friesnm);
else if (choice == 3)
printW.println("Customer's Order " + "\n" + quantity
+ friesnm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totalfries;
return totalfries;
}
public int Sundae() {
int choice, quantity = 0, sundae = 0, totalsun = 0;
String sundaenm = "";
String menu = "\nSUNDAE: \n1.Choco sundae ----> 28 \n2.Caramel sundae ----> 28 \n3.Strawberry sundae ----> 28";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
sundae += 28;
sundaenm = " Choco Sundae -";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
sundae += 28;
sundaenm = " Caramel Sundae ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 3:
sundae += 28;
sundaenm = " Strawberry Sundae - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totalsun = quantity * sundae;
sundaenm = sundaenm + totalsun + "\n";
JOptionPane.showMessageDialog(null, "Total = " + sundaenm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Costumer's Order " + "\n" + quantity
+ sundaenm);
} else if (choice == 2)
printW.println("Costumer's Order " + "\n" + quantity
+ sundaenm);
else if (choice == 3)
printW.println("Costumer's Order " + "\n" + quantity
+ sundaenm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totalsun;
return totalsun;
}
public int Drinks() {
int choice, quantity = 0, drinks = 0, totaldrinks = 0;
String drinksnm = "";
String menu = "\nDRINKS: \n1.Reg. Coke ----> 25 \n2.Large Coke ----> 35 \n3.Reg.Sprite ----> 28 \n4.Large Sprite ----> 38";
choice = Integer.parseInt(JOptionPane.showInputDialog("Please select: "
+ menu));
switch (choice) {
case 1:
drinks += 25;
drinksnm = "Regular Coke - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 2:
drinks += 35;
drinksnm = "Large Coke - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 3:
drinks += 28;
drinksnm = "Regular Sprite - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
case 4:
drinksnm = "Large Sprite - ";
quantity = Integer.parseInt(JOptionPane
.showInputDialog("How many do you want??? "));
break;
default:
JOptionPane.showMessageDialog(null, "INVALID");
}
if (quantity < 0) {
JOptionPane.showMessageDialog(null, "INVALID");
} else {
totaldrinks = quantity * drinks;
drinksnm = drinksnm + totaldrinks + "\n";
JOptionPane.showMessageDialog(null, "Total = " + drinksnm);
try {
PrintWriter printW = new PrintWriter(new FileWriter("cash.txt",
true));
if (choice == 1) {
printW.println("Costumer's Order " + "\n" + quantity
+ drinksnm);
} else if (choice == 2)
printW.println("Costumer's Order " + "\n" + quantity
+ drinksnm);
else if (choice == 3)
printW.println("Costumer's Order " + "\n" + quantity
+ drinksnm);
else if (choice == 4)
printW.println("Costumer's Order " + "\n" + quantity
+ drinksnm);
JOptionPane.showMessageDialog(null,
"Record successfully stored ");
printW.close();
} catch (Exception e) {
}
}
totalOrder = totalOrder + totaldrinks;
return totaldrinks;
}
public void orderList() {
try {
Scanner in = new Scanner(new FileReader("cash.txt"));
String all = "\nWELCOME !!!! \nCASHIERING SYSTEM\n";
while (in.hasNext()) {
all = all + in.nextLine() + "\n";
}
JOptionPane.showMessageDialog(null, new JTextArea(all));
in.close();
} catch (Exception e) {
}
}
public void getTotal() {
JOptionPane.showMessageDialog(null, "Order Total\n"+ totalOrder + "");
}
public void menu2() {
String menu[] = { "Burger", "Spaghetti", "Fries", "Ice Sundae",
"Drinks", "Exit" };
String choice = "";
char ch;
int yesorno = 0;
do {
choice = (String) JOptionPane.showInputDialog(null,
"Please select your order", "menu", 1, null, menu, menu[0]);
ch = choice.toLowerCase().charAt(0);
switch (ch) {
case 'b':
burger();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 's':
Spaghetti();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'f':
Fries();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'i':
Sundae();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'd':
Drinks();
yesorno = Integer
.parseInt(JOptionPane
.showInputDialog("Do you still have any order?? \n 1.Yes \n2.No"));
if (yesorno == 1) {
menu2();
} else {
Cashiering();
}
break;
case 'e':
break;
}
} while (!choice.equals("Close"));
}
}
I am trying to write a program that ask a user for 2 numbers and then ask the user to pick a command from a menu by entering the correspond number to the command.
I can write the program if i take the input as an Int but cannot figure it out for a string, also it has to be a string.
I am having problems when it enters the while loop to validate the user input it does not stop when the statement is false it will stay in the loop I can not figure out what i am doing wrong.
Here is the code i have.
import java.util.Scanner;
public class ab {
public static void main(String[] args) {
System.out.println("-------------------------------------");
Scanner stdIn = new Scanner(System.in);
double L;
System.out.print("Enter the left operand: ");
L = stdIn.nextDouble();
double R;
System.out.print("Enter the right operand: ");
R = stdIn.nextDouble();
System.out.println("-------------------------------------");
System.out.println("1 -> Multiplication");
System.out.println("2 -> Division");
System.out.println("3 -> Addition");
System.out.println("4 -> Subraction");
System.out.println("-------------------------------------");
String input;
System.out.print("Choose one of the following commands by enterning the corresponding number: ");
input = stdIn.next();
System.out.println();
while (!input.equals(1) && !input.equals(2) && !input.equals(3) && !input.equals(4)) {
System.out.print("Invalid entry, please type a valid number (1, 2, 3 or 4): ");
input = stdIn.next();
System.out.println();
if (input.equals(1)) {
System.out.print(L + " * " + R + " = " + (L * R));
} else if (input.equals(2)) {
System.out.print(L + " / " + R + " = " + (L / R));
} else if (input.equals(3)) {
System.out.print(L + " + " + R + " = " + (L + R));
} else {
System.out.print(L + " - " + R + " = " + (L - R));
}
}
stdIn.close();
}
}
Any help would be much appreciated.
Thank you in advanced.
The line input = stdIn.next(); is taking input as String
while your comparison is against integer. So a String never equals Int
You may try changing your while loop condition to:
while (!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4"))
note the double quote around the numbers
Is answered, but check this
import java.util.Scanner;
public class ab {
public static void main(String[] args) {
System.out.println("-------------------------------------");
Scanner stdIn = new Scanner(System.in);
double L;
System.out.print("Enter the left operand: ");
L = stdIn.nextDouble();
double R;
System.out.print("Enter the right operand: ");
R = stdIn.nextDouble();
System.out.println("-------------------------------------");
System.out.println("1 -> Multiplication");
System.out.println("2 -> Division");
System.out.println("3 -> Addition");
System.out.println("4 -> Subraction");
System.out.println("-------------------------------------");
String input;
System.out.print("Choose one of the following commands by enterning the corresponding number: ");
input = stdIn.next();
while (true) {
if (!input.equals("1") && !input.equals("2") && !input.equals("3") && !input.equals("4")) {
System.out.print("Invalid entry, please type a valid number (1, 2, 3 or 4): ");
input = stdIn.next();
} else {
if (input.equals("1")) {
System.out.print(L + " * " + R + " = " + (L * R));
break;
} else if (input.equals("2")) {
System.out.print(L + " / " + R + " = " + (L / R));
break;
} else if (input.equals("3")) {
System.out.print(L + " + " + R + " = " + (L + R));
break;
} else {
System.out.print(L + " - " + R + " = " + (L - R));
break;
}
}
}
stdIn.close();
}
}
So, I am creating a text-based game where you fill in the blanks on the board with the letters, "S, M, A, R, T". To beat this game each row and column can only contain 1 out of the 5 given letters. (It's kind of like the game Sudoku) The game runs perfectly fine but when the user inputs the the row and column they want the letter to be in, it doesn't appear on the board. Could anyone please help me out? Thank you in advance. (Note: I am still new to java coding)
import java.util.Scanner;
public class SmartPuzzleProgram{
static Scanner keyboard = new Scanner(System.in);
static char[][] table = new char[5][5];
public static void main (String[] args){
Board(table);
int i = 0;
while (i < 5){
if( ((int)table[i][0] + (int)table[i][1] + (int)table[i][2] + (int)table[i][3] + (int)table[i][4]) != 391 ){
Move();
Board(table);
}
else
i++;
}
int j = 0;
while (i < 5){
if( ((int)table[0][j] + (int)table[1][j] + (int)table[2][j] + (int)table[3][j] + (int)table[4][j]) != 391 ){
Move();
Board(table);
}
else
j++;
}
System.out.println("Congratulations! You must be SMART.");
}
public static void Move(){
// Ask for user's input
System.out.print("Enter a row (1-5): ");
int r = keyboard.nextInt();
System.out.print("Enter a column (1-5): ");
int c = keyboard.nextInt();
System.out.print("Enter a letter (S,M,A,R or T): ");
char L = keyboard.next().toUpperCase().charAt(0);
if (L == 'S' || L == 'M' || L == 'A' || L == 'R' || L == 'T') {
table[r-1][c-1] = L;
}
else
// Error check
System.out.println("Invalid letter. Use 'S', M', 'A', 'R' or 'T'");
}
public static void Board(char[][] t){
// Given variables
t[0][0] = 'S';
t[0][1] = 'M';
t[0][2] = 'A';
t[0][3] = 'R';
t[0][4] = 'T';
t[1][0] = ' ';
t[1][1] = 'T';
t[1][2] = 'S';
t[1][3] = 'M';
t[1][4] = ' ';
t[2][0] = ' ';
t[2][1] = ' ';
t[2][2] = 'R';
t[2][3] = ' ';
t[2][4] = 'S';
t[3][0] = ' ';
t[3][1] = 'S';
t[3][2] = 'M';
t[3][3] = ' ';
t[3][4] = ' ';
t[4][0] = ' ';
t[4][1] = ' ';
t[4][2] = 'T';
t[4][3] = 'S';
t[4][4] = ' ';
// Prints out the board
System.out.println(" 1 2 3 4 5 ");
System.out.println(" ---+---+---+---+--- ");
System.out.println("1 | " + t[0][0] + " | " + t[0][1] + " | " + t[0][2] + " | " + t[0][3] + " | " + t[0][4] + " |");
System.out.println(" ---+---+---+---+--- ");
System.out.println("2 | " + t[1][0] + " | " + t[1][1] + " | " + t[1][2] + " | " + t[1][3] + " | " + t[1][4] + " |");
System.out.println(" ---+---+---+---+--- ");
System.out.println("3 | " + t[2][0] + " | " + t[2][1] + " | " + t[2][2] + " | " + t[2][3] + " | " + t[2][4] + " |");
System.out.println(" ---+---+---+---+--- ");
System.out.println("4 | " + t[3][0] + " | " + t[3][1] + " | " + t[3][2] + " | " + t[3][3] + " | " + t[3][4] + " |");
System.out.println(" ---+---+---+---+--- ");
System.out.println("5 | " + t[4][0] + " | " + t[4][1] + " | " + t[4][2] + " | " + t[4][3] + " | " + t[4][4] + " |");
System.out.println(" ---+---+---+---+--- ");
}
}
The problem is that you are assigning the values to table each time you call the Board() method, you may want to initialize them just one time. For this, create a new method, let's say initialize():
public static void initialize()
{
System.out.println("here");
table[0][0] = 'S';
table[0][1] = 'M';
table[0][2] = 'A';
table[0][3] = 'R';
table[0][4] = 'T';
table[1][0] = ' ';
table[1][1] = 'T';
table[1][2] = 'S';
table[1][3] = 'M';
table[1][4] = ' ';
table[2][0] = ' ';
table[2][1] = ' ';
table[2][2] = 'R';
table[2][3] = ' ';
table[2][4] = 'S';
table[3][0] = ' ';
table[3][1] = 'S';
table[3][2] = 'M';
table[3][3] = ' ';
table[3][4] = ' ';
table[4][0] = ' ';
table[4][1] = ' ';
table[4][2] = 'T';
table[4][3] = 'S';
table[4][4] = ' ';
}
And call it at the beginning of the main():
public static void main(String[] args)
{
initialize();
Board(table);
...
}
I need to let the user input an operand, input 2 integers, display the result, and repeat. It wont repeat and when you input an integer to continue, it terminates. Any help would be appreciated, Ive been stuck for a while now! Thanks.
package lab03;
import java.util.Scanner;
import javax.swing.JOptionPane;
/**
*
*/
public class Lab03 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
int n, p, q = 1;
boolean run = true;
String operator;
while (run == true) {
operator = JOptionPane.showInputDialog("Enter one of + - * / ");
String trimOperator = operator.trim();
char m = trimOperator.charAt(0);
String firstOperand = JOptionPane
.showInputDialog("Enter first integer operand: ");
n = Integer.parseInt(firstOperand);
String secondOperand = JOptionPane
.showInputDialog("Enter second integer operand: ");
p = Integer.parseInt(secondOperand);
switch (m) {
case '+':
System.out.println(n + " plus " + p + " is " + (n + p));
run = false;
break;
case '-':
System.out.println(n + " minus " + p + " is " + (n - p));
run = false;
break;
case '*':
System.out
.println(n + " multiplied by " + p + " is " + (n * p));
run = false;
break;
case '/':
System.out.println(n + " divided by " + p + " is " + (n / p)
+ " with remainder " + (n % p));
run = false;
break;
default:
System.out.println("Invalid character");
run = false;
break;
}
String lastInteger = JOptionPane
.showInputDialog("Enter 0 to quit, or any other integer to continue. ");
q = Integer.parseInt(lastInteger);
System.out.println(q);
System.exit(0);
continue;
}
}
Sorry for lack of comments.