Calculator code errors

Page 1 of 1

9 Replies - 652 Views - Last Post: 12 November 2011 - 07:57 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=255389&amp;s=7dd299f2cd37be649a7361b80224a4cc&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 htek38

Reputation: 1
• Posts: 65
• Joined: 09-October 11

Calculator code errors

Posted 12 November 2011 - 05:34 PM

I have code for a simple calculator problem, but it is giving errors and I'm not sure why.

Here is my code:

```package mycalculator;

import javax.swing.JOptionPane;

public class MyCalculator {

public static void main(String[] args) {

String myInput = JOptionPane.showInputDialog("Enter an expression:");

String[] myExpression = myInput.split(" ");
for (int i = 0; i < myExpression.length; i++)
System.out.println("myExpression [" + i + "]= " + myExpression[i]);

if (myExpression.length != 3) {
System.out.println("Exit: (Please enter: operand1 operator operand2)");

System.exit(0);

myCalculation(myExpression);

}

public static void myCalculation(String[] myCalc) {

int result = 0;

for (int i = 0; i < myCalc.length; i++)

switch (myCalc[1].charAt(0)) {

case + : result = Integer.parseInt(myCalc[0]) +
Integer.parseInt(myCalc[2]);

break;

case - : result = Integer.parseInt(myCalc[0]) -
Integer.parseInt(myCalc[2]);

break;

case * : result = Integer.parseInt(myCalc[0]) *
Integer.parseInt(myCalc[2]);

break;

case / : result = Integer.parseInt(myCalc[0]) /
Integer.parseInt(myCalc[2]);

break;

}

System.out.println(myCalc[0] + " " + myCalc[1] + " " + myCalc[2] + " = " + result);

}
}
}
```

The errors that I am getting are "illegal start of expression" on lines 25, 33, 38, 43, and 48. And a different error on the very last closing curly bracket line 59 that error is "class, interface or enum expected".

I am not sure what to do to correct these errors and the error related to the curly bracket I've never even seen before.

Any help would be greatly appreciated.

Is This A Good Question/Topic? 0

Replies To: Calculator code errors

#2 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

Re: Calculator code errors

Posted 12 November 2011 - 06:54 PM

Line 12: You can't name a variable that. Fixes line 12, 14.
Line 31, 36, 41, 46: Use case '/': AND you forgot a { in the above for loop.

#3 htek38

Reputation: 1
• Posts: 65
• Joined: 09-October 11

Re: Calculator code errors

Posted 12 November 2011 - 07:02 PM

The html tags are not included in the code that I wrote...they were inserted for some reason when I posted.

Mylo, on 12 November 2011 - 07:54 PM, said:

Line 12: You can't name a variable that. Fixes line 12, 14.
Line 31, 36, 41, 46: Use case '/': AND you forgot a { in the above for loop.

Thank you for the curly brace. But i don't understand what you mean regarding the /.

#4 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

Re: Calculator code errors

Posted 12 November 2011 - 07:04 PM

Regarding the '/'.

Characters values are enclosed in a set of 's. So it is compared like that. Example:

```char test = 'z';
switch (test) {
case 'a':
// false
break;
case b:
// error - note the missing ''.
break;
case 'z':
// true
break;
}
```

This post has been edited by Mylo: 12 November 2011 - 07:07 PM

#5 htek38

Reputation: 1
• Posts: 65
• Joined: 09-October 11

Re: Calculator code errors

Posted 12 November 2011 - 07:06 PM

I understand regarding the single quotes around the case operators. That resolved those issues, but I still have the "illegal start of expression" error related to the method header though.

This post has been edited by htek38: 12 November 2011 - 07:10 PM

#6 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

Re: Calculator code errors

Posted 12 November 2011 - 07:12 PM

If your referring to mycalculations(). Then it is because you have an extra } at the bottom of your code.

#7 htek38

Reputation: 1
• Posts: 65
• Joined: 09-October 11

Re: Calculator code errors

Posted 12 November 2011 - 07:25 PM

Mylo, on 12 November 2011 - 08:12 PM, said:

If your referring to mycalculations(). Then it is because you have an extra } at the bottom of your code.

Here is my code as I have it now.

```package mycalculator;

import javax.swing.JOptionPane;

public class MyCalculator {

public static void main(String[] args) {

String myInput = JOptionPane.showInputDialog("Enter an expression:");

String[] myExpression = myInput.split(" ");
for (int i = 0; i < myExpression.length; i++)
System.out.println("myExpression [" + i + "] = " + myExpression[i]);

if (myExpression.length != 3) {
System.out.println("Exit: (Please enter: operand1 operator operand2)");

System.exit(0);

}

myCalculation(myExpression);

public static void myCalculation(String[] myCalc) {

int result = 0;

for (int i = 0; i < myCalc.length; i++) {

switch (myCalc[1].charAt(0)) {

case '+' : result = Integer.parseInt(myCalc[0]) +
Integer.parseInt(myCalc[2]);

break;

case '-' : result = Integer.parseInt(myCalc[0]) -
Integer.parseInt(myCalc[2]);

break;

case '*' : result = Integer.parseInt(myCalc[0]) *
Integer.parseInt(myCalc[2]);

break;

case '/' : result = Integer.parseInt(myCalc[0]) /
Integer.parseInt(myCalc[2]);

break;

}

System.out.println(myCalc[0] + " " + myCalc[1] + " " + myCalc[2] + " = " + result);

}
}
}
```

I'm still getting the same error related to line 25. "illegal start of expression". I would think if anything I would be missing a curly brace, but my compiler isn't giving an error related to that.

#8 Mylo

• Knows all, except most.

Reputation: 265
• Posts: 747
• Joined: 11-October 11

Re: Calculator code errors

Posted 12 November 2011 - 07:34 PM

You need a } to match your main method. Place under myCalculation(myExpression);

Compilers won't always return the exact error for things like curly braces as they think it is normal and that you just tried to declare a class in a method etc.

This post has been edited by Mylo: 12 November 2011 - 07:36 PM

#9 htek38

Reputation: 1
• Posts: 65
• Joined: 09-October 11

Re: Calculator code errors

Posted 12 November 2011 - 07:51 PM

Mylo, on 12 November 2011 - 08:34 PM, said:

You need a } to match your main method. Place under myCalculation(myExpression);

Compilers won't always return the exact error for things like curly braces as they think it is normal and that you just tried to declare a class in a method etc.

Thank you.
That solved the error regarding the method statement also. I now have an issue where my output is printing three times. I don't know where this is happening from.

My print output looks like this:

myExpression [0] = 5
myExpression [1] = -
myExpression [2] = 2
5 - 2 = 35 - 2 = 35 - 2 = 3BUILD SUCCESSFUL (total time: 8 seconds)

The output is correct until the last line with the two repeats.

#10 htek38

Reputation: 1
• Posts: 65
• Joined: 09-October 11

Re: Calculator code errors

Posted 12 November 2011 - 07:57 PM

I figured it out, I had my method print statement inside the for loop...thus causing the print statement to print three times.