Results 1 to 11 of 11
  1. #1
    AJ2009 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Question 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 12:02 PM.

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Add braces "{ }" around your if block. Without those only the first statement after the if statement is considered part of the if block.

  3. #3
    AJ2009 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    you mean the first if statement? can you specify? Thank you!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AJ2009 View Post
    you mean the first if statement? can you specify? Thank you!
    All if statements should have their blocks within curly braces, else you wouldn't have this problem.

  5. #5
    hind is offline Member
    Join Date
    Jan 2009
    Posts
    7
    Rep Power
    0

    Default

    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

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

  7. #7
    AJ2009 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    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);
    }
    }
    }
    }

  8. #8
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default 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.
    Java Code:
    itemPrice = Double.parseDouble (input);
    priceBeforeTax = Double.parseDouble (input);
    priceAfterTax = Double.parseDouble (input);
    Why are you assigning the input value to the these variables that get re-assigned other values later on?

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  9. #9
    AJ2009 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    Thank you. Everything is now working! Thanks for your help!
    Last edited by AJ2009; 01-04-2009 at 04:49 PM.

  10. #10
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Any time

    Welcome...

    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. Edit and compile
    By lmperator in forum New To Java
    Replies: 7
    Last Post: 01-03-2009, 06:15 PM
  2. Java 1.5 compile time error
    By ank_k in forum New To Java
    Replies: 4
    Last Post: 11-13-2008, 12:12 PM
  3. doesn't compile?!
    By jon80 in forum New To Java
    Replies: 8
    Last Post: 06-14-2008, 06:42 PM
  4. Not able to compile
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 11:13 PM
  5. compile error
    By dirtycash in forum New To Java
    Replies: 6
    Last Post: 12-12-2007, 07:00 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •