Results 1 to 11 of 11
Thread: Compile Error - Please Help!!
- 01-04-2009, 10:43 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
Compile Error - Please Help!!
I kept getting compile error.
'else' without 'if'
can somebody help me with my program? Thank you!:confused:
import java.text.*; // to use DecimalFormat
import java.util.*; // to use Scanner class
import javax.swing.*; // In order to use JOptionPane
public class orderSystem2
{
public static final double CHOICE_A = 1.50;
public static final double CHOICE_B = 2.00;
public static final double CHOICE_C = 2.50;
public static final double CHOICE_D = 3.00;
public static final double CHOICE_E = 3.50;
public static final double CHOICE_F = 4.00;
public static final double CHOICE_G = 4.50;
public static final double TAX_AMOUNT = 0.50;
public static void main(String[] args)
{
String choice, itemName, input;
int quantity;
double total, itemPrice,
priceBeforeTax, priceAfterTax;
Scanner keyboard = new Scanner (System.in);
//Display the food menu
choice =
JOptionPane.showInputDialog("Welcome to the Restruant!\n" +
"Here is today's special: \n" +
"A. Egg Rolls $1.50\n" +
"B. Deep Fried Wontons $2.00\n" +
"C. Egg Drop Soup $2.50\n" +
"D. Chow Mein (Fried noodles) $3.00\n" +
"E. GuangZhou Fried Rice $3.50\n" +
"F. Ma Po Tou Fu (Marpoo Dofu) $4.00\n" +
"G. Sweet and Sour Pork $4.50\n\n" +
"Please enter your order(A,B,C,D,E,F or G):");
choice = choice.toUpperCase();
if (! choice.equals ("A") &&
! choice.equals ("B") &&
! choice.equals ("C") &&
! choice.equals ("D") &&
! choice.equals ("E") &&
! choice.equals ("F") &&
! choice.equals ("G"))
itemPrice = Double.parseDouble (total);
JOptionPane.showMessageDialog (null,
"We don't have choice " + choice + " in our menu.\n" +
"The order has to be A,B,C,D,E,F or G");
else
{
input =
JOptionPane.showInputDialog("How many would you like to order:");
quantity = Integer.parseInt(input);
if (quantity <= 0)
{
JOptionPane.showMessageDialog (null,
"Error: Invalid quantity.\n");
}
else
{
// Display name of the Food
// Set price of the item
if (choice == "A")
{
itemName = " Egg Rolls";
itemPrice = CHOICE_A;
}
else if (choice == "B")
{
itemName = " Deep Fried Wontons";
itemPrice = CHOICE_B;
}
else if (choice == "C")
{
itemName = " Egg Drop Soup";
itemPrice = CHOICE_C;
}
else if (choice == "D")
{
itemName = " Chow Mein (Fried noodles)";
itemPrice = CHOICE_D;
}
else if (choice == "E")
{
itemName = " GuangZhou Fried Rice";
itemPrice = CHOICE_E;
}
else if (choice == "F")
{
itemName = " Ma Po Tou Fu (Marpoo Dofu)";
itemPrice = CHOICE_F;
}
else
{
itemName = " Sweet and Sour Pork";
itemPrice = CHOICE_G;
}
JOptionPane.showMessageDialog (null,
" You ordered " + quantity + itemName);
priceBeforeTax = itemPrice * quantity;
priceAfterTax = itemPrice * quantity * TAX_AMOUNT;
priceBeforeTax = Double.parseDouble (total);
priceAfterTax = Double.parseDouble (total);
DecimalFormat oneAfter = new DecimalFormat ("0.00");
JOptionPane.showMessageDialog(null,
"Price before tax is $" + oneAfter.format(priceBeforeTax)+
"\nTax is $" + TAX_AMOUNT +
"\nPrice after tax is $" + oneAfter.format(priceAfterTax) +
"\n\nThank you for your order and Happy NewYear!");
System.exit(0);
}
}
}
}Last edited by AJ2009; 01-04-2009 at 11:02 AM.
- 01-04-2009, 11:44 AM #2
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
Add braces "{ }" around your if block. Without those only the first statement after the if statement is considered part of the if block.
- 01-04-2009, 12:10 PM #3
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
you mean the first if statement? can you specify? Thank you!
-
- 01-04-2009, 02:22 PM #5
Member
- Join Date
- Jan 2009
- Posts
- 7
- Rep Power
- 0
hi
can someone help me please im new to java, i need to add checkboxs to the last column of my jtable so user can edit that column, all the other columns are populated with data read from txt file.
my code
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
// This program allows a list of Products to be maintained i.e.
// added, deleted and sorted. The products are displayed in
// a JTable and also maintained in an ArrayList.
// Some criticisms of the code:
// 1. Quite a lot of the code seems concerned with keeping the
// JTable and the ArrayList contents in step. Perhaps there
// is a better way to do this by writing my own
// TableModel.
// 2. No data validation is implmented
// 3. The design does not conform to the MVC pattern
// 4. Only one sort order (by product name) is possible.
// To make it possible to sort into other orders e.g.
// by price it would be necessary to use a Comparator object.
// 5. The ArrayList used does not use generics
class StockEnquiries implements Comparable {
private String name;
private int phone;
private String branch;
public StockEnquiries(String n, int p, String b) {
name = n;
phone = p;
branch = b;
}
public StockEnquiries() {
}
public String getName() {
return name;
}
public int getPhone() {
return phone;
}
public String getBranch() {
return branch;
}
// compareTo() allows Product instances in a container
// (e.g. an ArrayList) to be sorted by Collections.sort().
// It can only sort things in one order (e.g. ascending order of
// name). Need to investigate the use of Comparator objects to
// allow different sort orders to be defined.
public int compareTo(Object o) {
StockEnquiries temp = (StockEnquiries) o;
// Strings already implement compareTo() so just use that
return (name.compareTo(temp.name));
}
}
public class GUI1 extends JFrame {
// A container for the Product instances
ArrayList enquiries = new ArrayList();
// GUI bits and bobs
JPanel panInput = new JPanel(new GridLayout(2,1)),
panAdd = new JPanel(),
panDelSort = new JPanel();
JTextField txtName = new JTextField(10),
txtPhone = new JTextField(5);
JTextField txtBranch = new JTextField(100);
JButton btnAdd = new JButton("Add"),
btnDelete = new JButton("Delete"),
btnSort = new JButton("Sort Products");
JTable tab = new JTable();
// The DefaultTableModel will allow the contents of the
// JTable to be manipulated.
DefaultTableModel tabMod = new DefaultTableModel();
StockEnquiries [] initialEnquiries = {new StockEnquiries(),
new StockEnquiries(),
new StockEnquiries()};
public GUI1() {
super("stock enquiries list");
tab.setModel(tabMod);
tabMod.addColumn("Name");
tabMod.addColumn("Phone");
tabMod.addColumn("Branch");
tabMod.addColumn("time");
tabMod.addColumn("enquiry");
tabMod.addColumn("dealt with");
btnAdd.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
addStockEnquiries();
}
});
btnDelete.setToolTipText("Select the records to delete first");
btnDelete.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
deleteStockEnquiries();
}
});
btnSort.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
sortEnquiries();
}
});
panAdd.add(btnAdd);
panAdd.add(new JLabel("Product name:"));
panAdd.add(txtName);
panAdd.add(new JLabel("Phone Number:"));
panAdd.add(txtPhone);
panDelSort.add(btnDelete);
panDelSort.add(btnSort);
panInput.add(panAdd);
panInput.add(panDelSort);
getContentPane().add(panInput, BorderLayout.SOUTH);
getContentPane().add(new JScrollPane(tab));
loadInitialEnquiries();
}
// Delete a product from the ArrayList and from the JTable
public void deleteStockEnquiries() {
int [] rows = tab.getSelectedRows();
for (int i = 0; i < rows.length; i++) {
tabMod.removeRow(rows[i] - i); // delete from JTable
enquiries.remove(rows[i] - i); // delete from ArrayList
}
}
// Sort the products in the ArrayList and then
// reload them into the JTable in the sorted order
public void sortEnquiries() {
Collections.sort(enquiries); // sorts into name order
loadEnquiriesIntoTable();
}
// Add a new products to the ArrayList and the JTable
public void addStockEnquiries() {
String [] row = new String [4];
StockEnquiries newEnquiry = new StockEnquiries(txtName.getText(), Integer.parseInt(txtPhone.getText()), txtBranch.getText());
enquiries.add(newEnquiry); // add to the ArrayList
row[0] = newEnquiry.getName();
row[1] = Integer.toString(newEnquiry.getPhone());
row[2] = newEnquiry.getBranch();
tabMod.addRow(row); // add to the JTable
}
public void loadInitialEnquiries() {
BufferedReader br = null;
try {
br = new BufferedReader(
new FileReader("stockEnquiryLog.txt"));
String s;
String name = "";
String phone = "";
String branch = "";
String enquiry ="";
String time = "";
int colCount = tab.getColumnCount();
String[] row = new String[colCount];
while ((s = br.readLine()) != null) {
if(s.equals("<stock_enquiry>")) {
row[0] = br.readLine(); // name
row[1] = br.readLine(); // phone
row[2] = br.readLine(); // branch
row[3] = br.readLine(); // time
row[4] = br.readLine(); // enquiry
time = br.readLine();
tabMod.addRow(row);
}
// Premature closing of InputStream by
// this misplaced line:
// br.close();
}
// Close the Reader after we are done with it.
br.close();
} catch(IOException e) {
System.out.println("loadInitialEnquiries i/o error: " +
e.getMessage());
}
}
public void loadEnquiriesIntoTable() {
tabMod.setRowCount(0);
String [] row = new String [3];
for (Iterator i = enquiries.iterator(); i.hasNext();) {
StockEnquiries temp = (StockEnquiries) i.next();
row[0] = temp.getName();
row[1] = Integer.toString(temp.getPhone());
row[2] = temp.getBranch();
tabMod.addRow(row);
}
}
public static void main(String [] a) throws FileNotFoundException, IOException{
GUI1 me = new GUI1();
me.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0) ;
}
});
me.pack();
me.setVisible(true);
}
}
// i know i have to create a class to represent a thing called a TableModel,apart from this im clueless can someone help
thank you
- 01-04-2009, 02:41 PM #6
Senior Member
- Join Date
- Jun 2008
- Posts
- 2,366
- Rep Power
- 7
- 01-04-2009, 03:15 PM #7
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
Here is my modified code. there shows no error now.
but howcome I cann't get the calculation working. I mean for example when i pick item B. the system calculates only item G. the price and itemName i've set up for each item doesn't go into the formula.
import java.text.*; // to use DecimalFormat
import java.util.*; // to use Scanner class
import javax.swing.*; // In order to use JOptionPane
public class OrderSystem3
{
public static final double TAX_AMOUNT = 0.50;
public static void main(String[] args)
{
String choice, itemName, input, total;
int quantity;
double itemPrice, priceBeforeTax, priceAfterTax;
Scanner keyboard = new Scanner (System.in);
//Display the food menu
choice =
JOptionPane.showInputDialog("Welcome to the Chinese Restruant!\n" +
"Here is today's special: \n" +
"A. Egg Rolls $1.50\n" +
"B. Deep Fried Wontons $2.00\n" +
"C. Egg Drop Soup $2.50\n" +
"D. Chow Mein (Fried noodles) $3.00\n" +
"E. GuangZhou Fried Rice $3.50\n" +
"F. Ma Po Tou Fu (Marpoo Dofu) $4.00\n" +
"G. Sweet and Sour Pork $4.50\n\n" +
"Please enter your order(A,B,C,D,E,F or G):");
choice = choice.toUpperCase();
if (! choice.equals ("A") &&
! choice.equals ("B") &&
! choice.equals ("C") &&
! choice.equals ("D") &&
! choice.equals ("E") &&
! choice.equals ("F") &&
! choice.equals ("G"))
{
JOptionPane.showMessageDialog (null,
"We don't have choice " + choice + " in our menu.\n" +
"The order has to be A,B,C,D,E,F or G");
}
else
{
input =
JOptionPane.showInputDialog("How many would you like to order:");
quantity = Integer.parseInt(input);
itemPrice = Double.parseDouble (input);
if (quantity <= 0)
{
JOptionPane.showMessageDialog (null,
"Error: Invalid quantity.\n");
}
else
{
// Display name of the Food
// Set price of the item
if (choice == "A")
{
itemName = " Egg Rolls";
itemPrice = 1.50;
}
else if (choice == "B")
{
itemName = " Deep Fried Wontons";
itemPrice = 2.00;
}
else if (choice == "C")
{
itemName = " Egg Drop Soup";
itemPrice = 2.50;
}
else if (choice == "D")
{
itemName = " Chow Mein (Fried noodles)";
itemPrice = 3.00;
}
else if (choice == "E")
{
itemName = " GuangZhou Fried Rice";
itemPrice = 3.50;
}
else if (choice == "F")
{
itemName = " Ma Po Tou Fu (Marpoo Dofu)";
itemPrice = 4.00;
}
else
{
itemName = " Sweet and Sour Pork";
itemPrice = 4.50;
}
priceBeforeTax = Double.parseDouble (input);
priceAfterTax = Double.parseDouble (input);
JOptionPane.showMessageDialog (null,
" You ordered " + quantity + itemName);
priceBeforeTax = itemPrice * quantity;
priceAfterTax = priceBeforeTax + TAX_AMOUNT;
DecimalFormat oneAfter = new DecimalFormat ("0.00");
JOptionPane.showMessageDialog(null,
"Price before tax is $" + oneAfter.format(priceBeforeTax)+
"\nTax is $" + TAX_AMOUNT +
"\nPrice after tax is $" + oneAfter.format(priceAfterTax) +
"\n\nThank you for your order and Happy NewYear!");
System.exit(0);
}
}
}
}
- 01-04-2009, 03:35 PM #8
Questions & suggestions
if (choice == "A")
Use the .equals() method when compararing strings (like you did at the begining of your code). This is probably why any selection drops into the "G" catagory.
Why are you assigning the input value to the these variables that get re-assigned other values later on?Java Code:itemPrice = Double.parseDouble (input); priceBeforeTax = Double.parseDouble (input); priceAfterTax = Double.parseDouble (input);
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
- 01-04-2009, 03:44 PM #9
Member
- Join Date
- Jan 2009
- Posts
- 5
- Rep Power
- 0
Thank you. Everything is now working! Thanks for your help!
Last edited by AJ2009; 01-04-2009 at 03:49 PM.
- 01-04-2009, 03:56 PM #10
Any time
Welcome...
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
Similar Threads
-
Edit and compile
By lmperator in forum New To JavaReplies: 7Last Post: 01-03-2009, 05:15 PM -
Java 1.5 compile time error
By ank_k in forum New To JavaReplies: 4Last Post: 11-13-2008, 11:12 AM -
doesn't compile?!
By jon80 in forum New To JavaReplies: 8Last Post: 06-14-2008, 05:42 PM -
Not able to compile
By bugger in forum New To JavaReplies: 2Last Post: 01-09-2008, 10:13 PM -
compile error
By dirtycash in forum New To JavaReplies: 6Last Post: 12-12-2007, 06:00 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks