# Problems with calculator program

• 12-10-2013, 12:40 AM
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();

}
}
• 12-10-2013, 12:57 AM
superhaNds
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.
• 12-10-2013, 01:03 AM
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.
• 12-10-2013, 01:07 AM
superhaNds
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.
• 12-10-2013, 01:11 AM
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.
• 12-10-2013, 01:29 AM
CopiousNote\$
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
• 12-10-2013, 01:33 AM
Re: Problems with calculator program
Thanks, this fixed it. Funny how one simple mistake can mess everything up...
• 12-10-2013, 01:43 AM
CopiousNote\$
Re: Problems with calculator program
No worries, sometimes just need an extra set of eyes
• 12-10-2013, 05:14 AM
kavikodikara
Re: Problems with calculator program
Sample code for java Calculator

techmoood ..