# Thread: Problems with calculator program

1. Member
Join Date
Dec 2013
Posts
4
Rep Power
0

## Problems with calculator program

Hey,

I've been working on a calculator, trying to use some if/else statements. The way it should run is you enter the operation, then the numbers, and the program executes the operation and stops. However, when I run it and enter an operation, it does that operation, then goes on to do multiplication. I think it might have to do with the way I'm using the if/else statements, but could somebody let me know what I'm doing wrong here?

Thanks

package calculator;

import java.util.Scanner;

public class Calculator {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("What operation would you like to use?");

String operation;
operation = "";

operation = input.nextLine();

if (operation.equals ("+"))
{
Double x = input.nextDouble();

Double y = input.nextDouble();

}

else if (operation.equals ("-"))
{
Double x = input.nextDouble();

Double y = input.nextDouble();

}

if (operation.equals ("*"));
{
Double x = input.nextDouble();

Double y = input.nextDouble();

}

input.close();

}
}

2. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
5

## Re: Problems with calculator program

What do you mean it moves on to do the multiplication, that's not possible. you could use an else if on the last but it doesn't matter.

3. Member
Join Date
Dec 2013
Posts
4
Rep Power
0

## Re: Problems with calculator program

As in, I'll enter subtraction, then it'll do the subtraction, then complete the if statement for multiplication. I just tried an else if on the last statement, but it still does the same thing.

4. Senior Member
Join Date
Apr 2013
Location
Sweden
Posts
272
Rep Power
5

## Re: Problems with calculator program

I don't know and see how it is possible. I also ran the code. It works fine and your output is strange because if you enter addition the other else if clauses won't be even evaluated.
After any operation is complete the program is terminated.

5. Member
Join Date
Dec 2013
Posts
4
Rep Power
0

## Re: Problems with calculator program

Hmm. That's weird. So the code is fine, but the program is just running it strangely? I'm using NetBeans, by the way.

6. Member
Join Date
Dec 2013
Posts
2
Rep Power
0

## Re: Problems with calculator program

Hi,

I think your problem is this:

if (operation.equals ("*"));

You need to remove the ';' and it'll work. i.e. use if (operation.equals ("*")) instead.

CN

7. Member
Join Date
Dec 2013
Posts
4
Rep Power
0

## Re: Problems with calculator program

Thanks, this fixed it. Funny how one simple mistake can mess everything up...

8. Member
Join Date
Dec 2013
Posts
2
Rep Power
0

## Re: Problems with calculator program

No worries, sometimes just need an extra set of eyes

9. Member
Join Date
Dec 2013
Posts
1
Rep Power
0

## Re: Problems with calculator program

Sample code for java Calculator

techmoood ..

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•