| | |
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:
Solved Threads: 0
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.
//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.
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:
When initializing arrays you should use '{' '}' not '(' ')'
Also:
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
you should really use code tags when posting code, it is way to hard to read without...
Anyway, the errors I've spotted are:
java Syntax (Toggle Plain Text)
private String[] keys = ("7", "8", "9", "/", "4", "5", "6","*", "1", "2", "3", "-", "0", "C", "=", "+");
java Syntax (Toggle Plain Text)
private String[] keys = {"7", "8", "9", "/", "4", "5", "6","*", "1", "2", "3", "-", "0", "C", "=", "+"};
java Syntax (Toggle Plain Text)
this.addWindowListener(new Window Adapter()) { public void windowClosing(WindowEvent e) { System.exit(0); } } };
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
333 - halfway to hell
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)
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...
Java Syntax (Toggle Plain Text)
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"; } }//missing closing bracket for evaluate() method public static void main(String[] args) { Calculator C = new Calculator(); } }//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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Oct 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Oct 2009
Posts: 6
Reputation:
Solved Threads: 0
0
#6 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)
Java Syntax (Toggle Plain Text)
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"; } }//missing closing bracket for evaluate() method public static void main(String[] args) { Calculator C = new Calculator(); } }//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]
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.
-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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
![]() |
Similar Threads
- Inventory Program (Java)
- Inventory program (Java)
- Payroll Program 3 for IT215 (Java)
- Java Mortgage Calculator Program (Java)
- Identifier expected and Illegal start of Expression errors (Java)
- interface, or enum expected errors (Java)
- Inventory program part 5 help (Java)
- class, interface, or enum expected (Java)
Other Threads in the Java Forum
- Previous Thread: need help in converting a classic tictactoe game into J2ME
- Next Thread: HW help.
Views: 786 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
actuate android api apple applet application arguments array arrays automation balls binary bluetooth business chat class classes client code codesnippet collections component coordinates database defaultmethod doctype dragging draw ebook eclipse educational error event exception file fractal game givemetehcodez graphics gui helpwithhomework hql html ide image ingres input integer invokingapacheantprogrammatically j2me java javaprojects jmf jni jpanel julia linux list loop looping map method methods mobile mysql netbeans newbie number numbers object oracle parameter php print problem program programming project recursion scanner screen server set size sms socket sort sql string sun swing swt tcp test threads time transfer tree udp windows






