Calculator Program - class, interface, or enum expected

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Oct 2009
Posts: 6
Reputation: locked_twilight is an unknown quantity at this point 
Solved Threads: 0
locked_twilight locked_twilight is offline Offline
Newbie Poster

Calculator Program - class, interface, or enum expected

 
0
  #1
Oct 11th, 2009
package MyJava; //change package name according to what the name of the package
//the class would be installed to

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class Calculator extends JFrame implements ActionListener{

private JTextField displayText = new JTextField(30);
private JButton[] button = new JButton[16];

private String[] keys = {"7", "8", "9", "/",
"4", "5", "6","*",
"1", "2", "3", "-",
"0", "C", "=", "+"};

private String numStr1 = "";
private String numStr2 = "";

private char op;
private boolean firstInput = true;

public Calculator () {
setTitle("Calculator");
setSize(230, 200);
Container pane = getContentPane();

pane.setLayout(null);

displayText.setSize(200, 30);
displayText.setLocation(10,10);
pane.add(displayText);

int x, y;
x = 10;
y = 40;

for (int ind = 0; ind < 16; ind++) {
button[ind] = new JButton(keys[ind]);
button[ind].addActionListener(this);
button[ind].setSize(50, 30);
button[ind].setLocation(x, y);
pane.add(button[ind]);
x = x + 50;

if ((ind + 1) % 4 == 0) {
x = 10;
y = y +30;
}
}

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

setVisible(true);

}

public void actionPerformed(ActionEvent e) {
String resultStr;

String str = String.valueOf(e.getActionCommand());

char ch = str.charAt(0);

switch (ch) {
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
if (firstInput) {
numStr1 = numStr1 + ch;
displayText.setText(numStr1);
}
else {
numStr2 = numStr2 + ch;
displayText.setText(numStr2);
}
break;

case '+' :
case '-' :
case '*' :
case '/' :
op = ch;
firstInput = false;
break;
case '=' :
resultStr = evauate();
displayText.setText(resultStr);
numStr1 = resultStr;
numStr2 = "";
firstInput = false;
break;

case 'C' :
displayText.setText("");
numStr1 = "";
numStr2 = "";
firstInput = true;
}
}

private String evaluate() {
final char beep = '\u0007';

try {
int num1 = Integer.parseInt(numStr1);
int num2 = Integer.parseInt(numStr2);
int result = 0;

switch (op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 + num2;
}

return String.valueOf(result);
}
catch (ArithmeticException e) {
System.out.print(beep);
return: "E R R O R: " + e.getMessage();
}
catch (NumberFormatException e) {
System.out.print(beep);

if (numStr1.equals(""))
return "E R R O R : Invalid First Number";
else
return "E R R O R: Invalid Second Number";
}
catch (Exception e) {
System.out.print(beep);
return "E R R O R";
}


public static void main(String[] args) {
Calculator C = new Calculator();
}

----------
class, interface, or enum expected beginning at lines char ch = str.charAt(0); down to
catch (Exception e) {
System.out.print(beep);
return "E R R O R";
}

Please help me, I'm just a beginner in Java.
Thank you.
Last edited by locked_twilight; Oct 11th, 2009 at 11:19 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 1,030
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 153
JamesCherrill JamesCherrill is offline Offline
Veteran Poster
 
1
  #2
Oct 11th, 2009
Probably one too many } brackets. Repost code with code=java tags and corect indentation.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 54
Reputation: di2daer is an unknown quantity at this point 
Solved Threads: 12
di2daer's Avatar
di2daer di2daer is offline Offline
Junior Poster in Training
 
1
  #3
Oct 11th, 2009
Hi,

you should really use code tags when posting code, it is way to hard to read without...
Anyway, the errors I've spotted are:
  1. private String[] keys = ("7", "8", "9", "/",
  2. "4", "5", "6","*",
  3. "1", "2", "3", "-",
  4. "0", "C", "=", "+");
When initializing arrays you should use '{' '}' not '(' ')'
  1. private String[] keys = {"7", "8", "9", "/",
  2. "4", "5", "6","*",
  3. "1", "2", "3", "-",
  4. "0", "C", "=", "+"};
Also:
  1. this.addWindowListener(new Window Adapter()) {
  2. public void windowClosing(WindowEvent e) {
  3. System.exit(0);
  4. }
  5. }
  6. };
WindowAdapter should not be separated with a space. Also, when you write inner anonymous classes like this, you must write the whole class within the parenthesis of the method call.
this.addWindowListener(new Window Adapter()) <-- this should be moved to the end of the method call.

I also noticed that there is an ':' after a return statement in a catch clause.

There might be more, but I hope this should get you going
Good luck
---------------------------
333 - halfway to hell
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,272
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
0
  #4
Oct 11th, 2009
You need to pay closer attention to opening and closing of brackets. See bellow code (the code still will not compile and throw some errors which you need to solve)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5.  
  6. public class Calculator extends JFrame implements ActionListener{
  7.  
  8. private JTextField displayText = new JTextField(30);
  9. private JButton[] button = new JButton[16];
  10.  
  11. private String[] keys = {"7", "8", "9", "/",
  12. "4", "5", "6","*",
  13. "1", "2", "3", "-",
  14. "0", "C", "=", "+"};
  15.  
  16. private String numStr1 = "";
  17. private String numStr2 = "";
  18.  
  19. private char op;
  20. private boolean firstInput = true;
  21.  
  22. public Calculator () {
  23. setTitle("Calculator");
  24. setSize(230, 200);
  25. Container pane = getContentPane();
  26.  
  27. pane.setLayout(null);
  28.  
  29. displayText.setSize(200, 30);
  30. displayText.setLocation(10,10);
  31. pane.add(displayText);
  32.  
  33. int x, y;
  34. x = 10;
  35. y = 40;
  36.  
  37. for (int ind = 0; ind < 16; ind++) {
  38. button[ind] = new JButton(keys[ind]);
  39. button[ind].addActionListener(this);
  40. button[ind].setSize(50, 30);
  41. button[ind].setLocation(x, y);
  42. pane.add(button[ind]);
  43. x = x + 50;
  44.  
  45. if ((ind + 1) % 4 == 0) {
  46. x = 10;
  47. y = y +30;
  48. }
  49. }
  50.  
  51. this.addWindowListener(new Window Adapter()) {
  52. public void windowClosing(WindowEvent e) {
  53. System.exit(0);
  54. }
  55. }
  56. };
  57.  
  58. setVisible(true);
  59.  
  60. //}
  61.  
  62. public void actionPerformed(ActionEvent e) {
  63. String resultStr;
  64.  
  65. String str = String.valueOf(e.getActionCommand());
  66.  
  67. char ch = str.charAt(0);
  68.  
  69. switch (ch) {
  70. case '0' :
  71. case '1' :
  72. case '2' :
  73. case '3' :
  74. case '4' :
  75. case '5' :
  76. case '6' :
  77. case '7' :
  78. case '8' :
  79. case '9' :
  80. if (firstInput) {
  81. numStr1 = numStr1 + ch;
  82. displayText.setText(numStr1);
  83. }
  84. else {
  85. numStr2 = numStr2 + ch;
  86. displayText.setText(numStr2);
  87. }
  88. break;
  89.  
  90. case '+' :
  91. case '-' :
  92. case '*' :
  93. case '/' :
  94. op = ch;
  95. firstInput = false;
  96. break;
  97. case '=' :
  98. resultStr = evauate();
  99. displayText.setText(resultStr);
  100. numStr1 = resultStr;
  101. numStr2 = "";
  102. firstInput = false;
  103. break;
  104.  
  105. case 'C' :
  106. displayText.setText("");
  107. numStr1 = "";
  108. numStr2 = "";
  109. firstInput = true;
  110. }
  111. }
  112.  
  113. private String evaluate() {
  114. final char beep = '\u0007';
  115.  
  116. try {
  117. int num1 = Integer.parseInt(numStr1);
  118. int num2 = Integer.parseInt(numStr2);
  119. int result = 0;
  120.  
  121. switch (op) {
  122. case '+':
  123. result = num1 + num2;
  124. break;
  125. case '-':
  126. result = num1 - num2;
  127. break;
  128. case '*':
  129. result = num1 * num2;
  130. break;
  131. case '/':
  132. result = num1 + num2;
  133. }
  134.  
  135. return String.valueOf(result);
  136. }
  137. catch (ArithmeticException e) {
  138. System.out.print(beep);
  139. return: "E R R O R: " + e.getMessage();
  140. }
  141. catch (NumberFormatException e) {
  142. System.out.print(beep);
  143.  
  144. if (numStr1.equals(""))
  145. return "E R R O R : Invalid First Number";
  146. else
  147. return "E R R O R: Invalid Second Number";
  148. }
  149. catch (Exception e) {
  150. System.out.print(beep);
  151. return "E R R O R";
  152. }
  153. }//missing closing bracket for evaluate() method
  154.  
  155. public static void main(String[] args) {
  156. Calculator C = new Calculator();
  157. }
  158. }//missing closing bracket for class

PS: In the future please code tags as [code]YOUR CODE HERE[/code] or [code=java]YOUR CODE HERE[/code]

PS2: Looks like di2daer already gave you some pointers in regards of code that I wanted you to solve, well your life is little easier now...
Last edited by peter_budo; Oct 11th, 2009 at 11:30 am.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 6
Reputation: locked_twilight is an unknown quantity at this point 
Solved Threads: 0
locked_twilight locked_twilight is offline Offline
Newbie Poster
 
0
  #5
Oct 11th, 2009
When I thought about my program again, I got it fixed and my younger brother helped me(how embarrassing).

And yes, those errors are right:
The brackets, the String keys, and the WindowAdapter.
I just fixed it a minute ago.

The class, interface, enum error is because I placed some of my methods outside the class, which is illegal.

Thanks, di2daer and James. =)

And sorry for the wrong pasting of code.
Last edited by locked_twilight; Oct 11th, 2009 at 11:29 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 6
Reputation: locked_twilight is an unknown quantity at this point 
Solved Threads: 0
locked_twilight locked_twilight is offline Offline
Newbie Poster
 
0
  #6
Oct 11th, 2009
Originally Posted by peter_budo View Post
You need to pay closer attention to opening and closing of brackets. See bellow code (the code still will not compile and throw some errors which you need to solve)
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.io.*;
  5.  
  6. public class Calculator extends JFrame implements ActionListener{
  7.  
  8. private JTextField displayText = new JTextField(30);
  9. private JButton[] button = new JButton[16];
  10.  
  11. private String[] keys = {"7", "8", "9", "/",
  12. "4", "5", "6","*",
  13. "1", "2", "3", "-",
  14. "0", "C", "=", "+"};
  15.  
  16. private String numStr1 = "";
  17. private String numStr2 = "";
  18.  
  19. private char op;
  20. private boolean firstInput = true;
  21.  
  22. public Calculator () {
  23. setTitle("Calculator");
  24. setSize(230, 200);
  25. Container pane = getContentPane();
  26.  
  27. pane.setLayout(null);
  28.  
  29. displayText.setSize(200, 30);
  30. displayText.setLocation(10,10);
  31. pane.add(displayText);
  32.  
  33. int x, y;
  34. x = 10;
  35. y = 40;
  36.  
  37. for (int ind = 0; ind < 16; ind++) {
  38. button[ind] = new JButton(keys[ind]);
  39. button[ind].addActionListener(this);
  40. button[ind].setSize(50, 30);
  41. button[ind].setLocation(x, y);
  42. pane.add(button[ind]);
  43. x = x + 50;
  44.  
  45. if ((ind + 1) % 4 == 0) {
  46. x = 10;
  47. y = y +30;
  48. }
  49. }
  50.  
  51. this.addWindowListener(new Window Adapter()) {
  52. public void windowClosing(WindowEvent e) {
  53. System.exit(0);
  54. }
  55. }
  56. };
  57.  
  58. setVisible(true);
  59.  
  60. //}
  61.  
  62. public void actionPerformed(ActionEvent e) {
  63. String resultStr;
  64.  
  65. String str = String.valueOf(e.getActionCommand());
  66.  
  67. char ch = str.charAt(0);
  68.  
  69. switch (ch) {
  70. case '0' :
  71. case '1' :
  72. case '2' :
  73. case '3' :
  74. case '4' :
  75. case '5' :
  76. case '6' :
  77. case '7' :
  78. case '8' :
  79. case '9' :
  80. if (firstInput) {
  81. numStr1 = numStr1 + ch;
  82. displayText.setText(numStr1);
  83. }
  84. else {
  85. numStr2 = numStr2 + ch;
  86. displayText.setText(numStr2);
  87. }
  88. break;
  89.  
  90. case '+' :
  91. case '-' :
  92. case '*' :
  93. case '/' :
  94. op = ch;
  95. firstInput = false;
  96. break;
  97. case '=' :
  98. resultStr = evauate();
  99. displayText.setText(resultStr);
  100. numStr1 = resultStr;
  101. numStr2 = "";
  102. firstInput = false;
  103. break;
  104.  
  105. case 'C' :
  106. displayText.setText("");
  107. numStr1 = "";
  108. numStr2 = "";
  109. firstInput = true;
  110. }
  111. }
  112.  
  113. private String evaluate() {
  114. final char beep = '\u0007';
  115.  
  116. try {
  117. int num1 = Integer.parseInt(numStr1);
  118. int num2 = Integer.parseInt(numStr2);
  119. int result = 0;
  120.  
  121. switch (op) {
  122. case '+':
  123. result = num1 + num2;
  124. break;
  125. case '-':
  126. result = num1 - num2;
  127. break;
  128. case '*':
  129. result = num1 * num2;
  130. break;
  131. case '/':
  132. result = num1 + num2;
  133. }
  134.  
  135. return String.valueOf(result);
  136. }
  137. catch (ArithmeticException e) {
  138. System.out.print(beep);
  139. return: "E R R O R: " + e.getMessage();
  140. }
  141. catch (NumberFormatException e) {
  142. System.out.print(beep);
  143.  
  144. if (numStr1.equals(""))
  145. return "E R R O R : Invalid First Number";
  146. else
  147. return "E R R O R: Invalid Second Number";
  148. }
  149. catch (Exception e) {
  150. System.out.print(beep);
  151. return "E R R O R";
  152. }
  153. }//missing closing bracket for evaluate() method
  154.  
  155. public static void main(String[] args) {
  156. Calculator C = new Calculator();
  157. }
  158. }//missing closing bracket for class

PS: In the future please code tags as [code]YOUR CODE HERE[/code] or [code=java]YOUR CODE HERE[/code]
Really sorry for the wrong post...
And I resolved it myself, thanks... Sorry to bother you Peter_budo.
I was just too overwhelmed(I have not coded for weeks).

And the replies here are a lot faster than I thought.
Really, really thanks to all of you guys again.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,272
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 494
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer
 
-1
  #7
Oct 11th, 2009
You been lucky as there seems to be few people around. During the work week it may take few hours.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 6
Reputation: locked_twilight is an unknown quantity at this point 
Solved Threads: 0
locked_twilight locked_twilight is offline Offline
Newbie Poster
 
0
  #8
Oct 11th, 2009
Yeah, luckily it's Sunday.

I need to submit this tomorrow. O_o
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 786 | Replies: 7
Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC