Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
Liutauras Vilda
Jeanne Boyarsky
paul wheaton
Sheriffs:
Ron McLeod
Devaka Cooray
Henry Wong
Saloon Keepers:
Tim Holloway
Stephan van Hulst
Carey Brown
Tim Moores
Mikalai Zaikin
Bartenders:
Frits Walraven
Forum:
Beginning Java
help
Jade Davidson
Ranch Hand
Posts: 64
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I must be overlooking something obvious, but I don't know why I am getting "else without if" compiler errors.
<font size=2.5> import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class CalcApplet extends Applet implements MouseListener { boolean first_num_flag == true; String display = "0"; int num1, num2 = 0; char Op; int result = 0; //Declare the text field to be used for input and output: private TextField textInput = new TextField(""); //Declare the buttons of the calculator: private Button btn1 = new Button("1"); private Button btn2 = new Button("2"); private Button btn3 = new Button("3"); private Button btnDiv = new Button("/"); private Button btn4 = new Button("4"); private Button btn5 = new Button("5"); private Button btn6 = new Button("6"); private Button btnMult = new Button("*"); private Button btn7 = new Button("7"); private Button btn8 = new Button("8"); private Button btn9 = new Button("9"); private Button btnSub = new Button("-"); private Button btn0 = new Button("0"); private Button btnClear = new Button("C"); private Button btnEqual = new Button("="); private Button btnAdd = new Button("+"); private String sSavedValue; private char charPreviousCalc; public void init() { //Declare a panel to group all the calculator buttons together: Panel calcPanel = new Panel(); calcPanel.setLayout(new GridLayout(4,4)); calcPanel.add(btn1); calcPanel.add(btn2); calcPanel.add(btn3); calcPanel.add(btnDiv); calcPanel.add(btn4); calcPanel.add(btn5); calcPanel.add(btn6); calcPanel.add(btnMult); calcPanel.add(btn7); calcPanel.add(btn8); calcPanel.add(btn9); calcPanel.add(btnSub); calcPanel.add(btn0); calcPanel.add(btnClear); calcPanel.add(btnEqual); calcPanel.add(btnAdd); btn1.addMouseListener(this); btn2.addMouseListener(this); btn3.addMouseListener(this); btnDiv.addMouseListener(this); btn4.addMouseListener(this); btn5.addMouseListener(this); btn6.addMouseListener(this); btnMult.addMouseListener(this); btn7.addMouseListener(this); btn8.addMouseListener(this); btn9.addMouseListener(this); btnSub.addMouseListener(this); btn0.addMouseListener(this); btnClear.addMouseListener(this); btnEqual.addMouseListener(this); btnAdd.addMouseListener(this); //Add the panel & text field to the applet in a border layout: setLayout(new BorderLayout()); add(textInput,BorderLayout.NORTH); add(calcPanel,BorderLayout.CENTER); } public void mousePressed(MouseEvent event) { Object source = event.getSource(); if (source == btn0) if (first_num_flag == true) display = "0"; first_num_flag = false; else display += "0"; textInput.setText(display); if (source == btn1) if (first_num_flag == true) display = "1"; first_num_flag = false; else display += "1"; textInput.setText(display); if (source == btn2) if (first_num_flag == true) display = "2"; first_num_flag = false; else display += "2"; textInput.setText(display); if (source == btn3) if (first_num_flag == true) display = "3"; first_num_flag = false; else display += "3"; textInput.setText(display); if (source == btn4) if (first_num_flag == true) display = "4"; first_num_flag = false; else display += "4"; textInput.setText(display); if (source == btn5) if (first_num_flag == true) display = "5"; first_num_flag = false; else display += "5"; textInput.setText(display); if (source == btn6) if (first_num_flag == true) display = "6"; first_num_flag = false; else display += "6"; textInput.setText(display); if (source == btn7) if (first_num_flag == true) display = "7"; first_num_flag = false; else display += "7"; textInput.setText(display); if (source == btn8) if (first_num_flag == true) display = "8"; first_num_flag = false; else display += "8"; textInput.setText(display); if (source == btn9) if (first_num_flag == true) display = "9"; first_num_flag = false; else display += "9"; textInput.setText(display); else if (source == btnAdd) { first_num_flag = true; num1 = Integer.parseInt(display); Op = '+'; } else if (source == btnSub) { first_num_flag = true; num1 = Integer.parseInt(display); Op = '-'; } else if (source == btnMult) { first_num_flag = true; num1 = Integer.parseInt(display); Op = '*'; } else if (source == btnDiv) { first_num_flag = true; num1 = Integer.parseInt(display); Op = '/'; } else if (source == btnEqual) { num2 = Integer.parseInt(display); switch(Op) { case '+': result = num1 + num2; break; case '-': result = num1 - num2; break; case '*': result = num1 * num2; break; case '/': result = num1 / num2; } num2 = result; first_num_flag = true; } } public void mouseClicked(MouseEvent event) { } public void mouseReleased(MouseEvent event) { } public void mouseEntered(MouseEvent event) { } public void mouseExited(MouseEvent event) { } }
(edited by Cindy to format code)
[This message has been edited by Cindy Glass (edited December 14, 2001).]
David O'Meara
Rancher
Posts: 13459
I like...
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Ack!
Go to the effort of including brackets even for single line statements, you'll save on so much stress in the long run.
Bit hard to see your layout since you didn't use the UBB [ code] tags, but I recommend lining up the start and end braces.
public void myMethod() throws Exception { if(statement) { System.out.println("I'm here, no confusion"); } else { throw new Exception("Everything is fine, thanks."); } }
Dave.
William Barnes
Ranch Hand
Posts: 1067
2
I like...
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
First problem:
boolean first_num_flag == true;
Second problem:
if (source == btn0) if (first_num_flag == true) display = "0"; first_num_flag = false; else
If you wrote this with 1] an editor that indented automatically, or 2] used {} correctly you would see the problem
if (source == btn0) { if (first_num_flag == true) { display = "0"; } first_num_flag = false; // <--------------------------*** else
Please ignore post, I have no idea what I am talking about.
Jade Davidson
Ranch Hand
Posts: 64
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
OK, I see it now. thanks.
If a regular clown is funny, then a larger clown would be funnier. Math. Verified by this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Calculator - Stacks
problem with program
calculator applet
what each part of this program does
another question on calculator applet
Gift giving made easy with the permaculture playing cards
More...