Results 1 to 9 of 9
  1. #1
    Madhatter is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default 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 ("+"))
    {
    System.out.println("Please enter your first number.");
    Double x = input.nextDouble();

    System.out.println("Please enter your second number.");
    Double y = input.nextDouble();

    System.out.println("Your answer is " + (x + y));
    }

    else if (operation.equals ("-"))
    {
    System.out.println("Please enter your first number.");
    Double x = input.nextDouble();

    System.out.println("Please enter your second number.");
    Double y = input.nextDouble();

    System.out.println("Your answer is " + (x - y));
    }

    if (operation.equals ("*"));
    {
    System.out.println("Please enter your first number.");
    Double x = input.nextDouble();

    System.out.println("Please enter your second number.");
    Double y = input.nextDouble();

    System.out.println("Your answer is " + (x * y));
    }

    input.close();

    }
    }

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default 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. #3
    Madhatter is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default 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. #4
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    265
    Rep Power
    2

    Default 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. #5
    Madhatter is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default 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. #6
    CopiousNote$ is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default 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. #7
    Madhatter is offline Member
    Join Date
    Dec 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problems with calculator program

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

  8. #8
    CopiousNote$ is offline Member
    Join Date
    Dec 2013
    Posts
    2
    Rep Power
    0

    Default Re: Problems with calculator program

    No worries, sometimes just need an extra set of eyes

  9. #9
    kavikodikara is offline Member
    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Default Re: Problems with calculator program

    Sample code for java Calculator

    techmoood ..

Similar Threads

  1. Mod: Help with calculator program
    By harry.only1 in forum AWT / Swing
    Replies: 2
    Last Post: 04-28-2013, 12:44 AM
  2. Error in calculator program
    By Programmer3 in forum New To Java
    Replies: 3
    Last Post: 10-02-2012, 12:49 PM
  3. Calculator Program
    By CODinacup in forum New To Java
    Replies: 4
    Last Post: 09-27-2012, 11:35 PM
  4. Problem with if statement in a calculator program
    By peterhabe in forum New To Java
    Replies: 2
    Last Post: 08-26-2011, 05:07 PM
  5. Calculator program
    By kevzspeare in forum New To Java
    Replies: 6
    Last Post: 03-18-2009, 01:43 PM

Posting Permissions

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