Results 1 to 4 of 4
  1. #1
    V2001Gordon is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default unreachable statement - Java calculator program

    Hello - long time reader, first time poster! I would greatly appreciate any help with the "unreachable statement" error I am getting when compiling this application! (am getting it on the line that says keypad.add(keys[12]); //subtract (I thought maybe there was a missing return statement.. but no..?) Thank you for any insights offered!:confused:

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;
    import java.text.DecimalFormat;
    import javax.swing.JOptionPane;

    public class Calculator extends Frame implements ActionListener
    {
    private Button keys[];
    private Panel keypad;
    private TextField lcd;
    private double op1;
    private boolean first;
    private boolean foundKey;
    private boolean clearText;
    private int lastOp;
    private DecimalFormat calcPattern;

    public Calculator()
    {
    //mnu
    MenuBar mnuBar = new MenuBar();
    setMenuBar(mnuBar);

    //file mnu
    Menu mnuFile = new Menu("File",true);
    mnuBar.add(mnuFile);
    MenuItem mnuFileExit = new MenuItem("Exit");
    mnuFile.add(mnuFileExit);

    //edit mnu
    Menu mnuEdit = new Menu("Edit",true);
    mnuBar.add(mnuEdit);
    MenuItem mnuEditClear = new MenuItem("Clear");
    mnuEdit.add(mnuEditClear);
    mnuEdit.insertSeparator(1);
    MenuItem mnuEditCopy = new MenuItem("Copy");
    mnuEdit.add(mnuEditCopy);
    MenuItem mnuEditPaste = new MenuItem ("Paste");
    mnuEdit.add(mnuEditPaste);

    //about mnu
    Menu mnuAbout = new Menu("About",true);
    mnuBar.add(mnuAbout);
    MenuItem mnuAboutCalculator = new MenuItem("About");
    mnuAbout.add(mnuAboutCalculator);

    //ActionListener
    mnuFileExit.addActionListener(this);
    mnuEditClear.addActionListener(this);
    mnuEditCopy.addActionListener(this);
    mnuEditPaste.addActionListener(this);
    mnuAboutCalculator.addActionListener(this);

    //ActionCommand
    mnuFileExit.setActionCommand("Exit");
    mnuEditClear.setActionCommand("Clear");
    mnuEditCopy.setActionCommand("Copy");
    mnuEditPaste.setActionCommand("Paste");
    mnuAboutCalculator.setActionCommand("About");

    //init
    lcd = new TextField(20);
    lcd.setEditable(false);
    keypad = new Panel();
    keys = new Button[16];
    first = true;
    op1 = 0.0;
    clearText = true;
    lastOp = 0;
    calcPattern = new DecimalFormat("########.########");

    //butt. capt.
    for (int i=0; i<=9; i++)
    keys[i] = new Button(String.valueOf(i));

    keys[10] = new Button("/");
    keys[11] = new Button("*");
    keys[12] = new Button("-");
    keys[13] = new Button("+");
    keys[14] = new Button("=");
    keys[15] = new Button(".");

    //frame
    setLayout(new BorderLayout());
    keypad.setLayout(new GridLayout(4,4,10,10));

    for (int i=7; i<=10; i++) //7-9 div
    keypad.add(keys[i]);

    for (int i=4; i<=6; i++) // 4-6
    keypad.add(keys[i]);

    keypad.add(keys[11]); //mult

    for (int i=1; 1<=3; i++) //1-3
    keypad.add(keys[i]);

    keypad.add(keys[12]); //subt

    keypad.add(keys[0]); //0

    for (int i=15; i>=13; i--)
    keypad.add(keys[i]); //.,=,+

    for (int i=0; i<keys.length; i++)
    keys[i].addActionListener(this);

    add(lcd, BorderLayout.NORTH);
    add(keypad, BorderLayout.CENTER);

    addWindowListener(
    new WindowAdapter()
    {
    public void windowClosing(WindowEvent e)
    {
    System.exit(0);
    }
    }
    );

    } //end const.

    public void actionPerformed(ActionEvent e)
    {
    //click
    String arg = e.getActionCommand();
    if(arg == "Exit")
    System.exit(0);

    if(arg == "Clear")
    {
    clearText = true;
    first = true;
    op1 = 0.0;
    lcd.setText("");
    lcd.requestFocus();
    }

    if (arg == "Copy")
    {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    StringSelection contents = new StringSelection(lcd.getText());
    cb.setContents(contents, null);
    }

    if (arg == "Paste")
    {
    Clipboard cb = Toolkit.getDefaultToolkit().getSystemClipboard();
    Transferable content = cb.getContents(this);
    try
    {
    String s= (String)content.getTransferData(DataFlavor.stringF lavor);
    lcd.setText(calcPattern.format(Double.parseDouble( s)));
    }
    catch (Throwable exc)
    {
    lcd.setText("");
    }

    return;
    }

    if (arg == "About")
    {
    String message = "Calculator ver. 1.0\nOpenExhibit Software \nCopyright 2007\nAll rights reserved";
    JOptionPane.showMessageDialog(null,message,"About Calculator",JOptionPane.INFORMATION_MESSAGE);
    }

    //buttonclk
    foundKey = false;

    //clkd key
    for (int i=0; i<keys.length && !foundKey; i++)
    {
    if(e.getSource() == keys[i])
    {
    foundKey = true;
    switch(i)
    {
    //num dec btns
    case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 15:
    if (clearText)
    {
    lcd.setText("");
    clearText = false;
    }
    lcd.setText(lcd.getText() + keys[i].getLabel());
    break;

    //op butns
    case 10: case 11: case 12: case 13: case 14:
    clearText = true;

    if (first) //1st op
    {
    if(lcd.getText().length()==0) op1 = 0.0;
    else op1 = Double.parseDouble(lcd.getText());

    first = false;
    clearText = true;
    lastOp = i; //save op
    }
    else //2nd op
    {
    switch(lastOp)
    {
    case 10: //div
    op1 /= Double.parseDouble(lcd.getText());
    break;
    case 11: // *
    op1 *= Double.parseDouble(lcd.getText());
    break;
    case 12: // -
    op1 -= Double.parseDouble(lcd.getText());
    break;
    case 13: //+
    op1 += Double.parseDouble(lcd.getText());
    break;
    } //endswitch
    lcd.setText(calcPattern.format(op1));
    clearText = true;

    if(i==14) first = true; //=
    else lastOp = i; //save last
    } //end else
    break;
    } //end swtch i
    } //endif
    } //endfor
    } //endaction

    public static void main(String args[])
    {
    //set frame properties
    Calculator f = new Calculator();
    f.setTitle("Calculator");
    f.setBounds(200,200,300,300);
    f.setVisible(true);

    //img
    Image icon = Toolkit.getDefaultToolkit().getImage("calcImage.gi f");
    f.setIconImage(icon);
    } //end mn
    } //end class

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Java Code:
    for (int i = 1; 1 <= 3; i++)
    look carefully at this loop. See anything wrong with it? This loop will never end. Why?

    (you'll smack your head when you figure this out).

    Also, why not move up to Swing and leave out of date AWT behind?

    A moral to this problem is that when you encounter this type of error, you will need to look at a line or two above the error to see why it is occurring. Also, if my hint doesn't help you solve this, please post back and someone will give the answer.

    a final (I think) suggestion: put all for / while loops and if / else / ... blocks within curly braces even if only one line long. This isn't the cause of your current problem, but will likely be a cause of a future problem if you don't do this.

    Best of luck.
    Last edited by Fubarable; 12-13-2008 at 12:28 AM.

  3. #3
    V2001Gordon is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default D'oh!

    THANKS... that is what happens when you look and look and overlook some more! That pesky "1" vs "i"... thanks for saving my sanity! (And yes, I did smack my head...)

    And also thank you for the suggestions. I think I will try this also with Swing and try to "neat up" my code as you mentioned, too.

    Quote Originally Posted by Fubarable View Post
    Java Code:
    for (int i = 1; 1 <= 3; i++)
    look carefully at this loop. See anything wrong with it? This loop will never end. Why?

    (you'll smack your head when you figure this out).

    Also, why not move up to Swing and leave out of date AWT behind?

    A moral to this problem is that when you encounter this type of error, you will need to look at a line or two above the error to see why it is occurring. Also, if my hint doesn't help you solve this, please post back and someone will give the answer.

    a final (I think) suggestion: put all for / while loops and if / else / ... blocks within curly braces even if only one line long. This isn't the cause of your current problem, but will likely be a cause of a future problem if you don't do this.

    Best of luck.

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

    Default

    You're welcome.

    The reason I recommend the curly braces has little to do with neatness actually. Imagine you have this code:

    Java Code:
    int i = 0;
    for (i = 0; i < myButtons.length; i++)
      myButtons[i] = new JButton("Button " + i);
    and you want to change it so that the JButtons are added to a JPanel. If you don't use curly braces, bad things can happen here:
    Java Code:
    int i = 0;
    for (i = 0; i < myButtons.length; i++)
      myButtons[i] = new JButton("Button " + i);
      myPanel.add(myButtons[i]); // this code isn't doing what you want it to be doing.
    much better is this:
    Java Code:
    for (int i = 0; i < myButtons.length; i++)
    {
      myButtons[i] = new JButton("Button " + i);
      myPanel.add(myButtons[i]); // this code isn't doing what you want it to be doing.
    }

Similar Threads

  1. calculator java following pemdas rule
    By sassygirl_kea9@yahoo.com in forum New To Java
    Replies: 2
    Last Post: 07-10-2008, 04:15 AM
  2. Java Calculator
    By aapanju in forum New To Java
    Replies: 3
    Last Post: 04-17-2008, 05:33 AM
  3. Java calculator decimal
    By cart1443 in forum New To Java
    Replies: 2
    Last Post: 04-16-2008, 01:19 PM
  4. JavaScript to Java Statement
    By mutuah in forum Advanced Java
    Replies: 0
    Last Post: 08-08-2007, 05:16 AM
  5. Create a Calculator in Java
    By Albert in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:01 AM

Posting Permissions

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