9 Replies - 323 Views - Last Post: 12 November 2011 - 07:57 PM Rate Topic: -----

#1 htek38  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 htek38  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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.

View PostMylo, 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 /.
Was This Post Helpful? 0
  • +
  • -

#4 Mylo  Icon User is offline

  • Knows all, except most.

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

Re: Calculator code errors

Posted 12 November 2011 - 07:04 PM

Had a feeling they were.

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

Was This Post Helpful? 1
  • +
  • -

#5 htek38  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#6 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 htek38  Icon User is offline

  • D.I.C Head

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

Re: Calculator code errors

Posted 12 November 2011 - 07:25 PM

View PostMylo, 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.
Was This Post Helpful? 0
  • +
  • -

#8 Mylo  Icon User is offline

  • Knows all, except most.

Reputation: 265
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#9 htek38  Icon User is offline

  • D.I.C Head

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

Re: Calculator code errors

Posted 12 November 2011 - 07:51 PM

View PostMylo, 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.
Was This Post Helpful? 0
  • +
  • -

#10 htek38  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1