-2

I'm new to Java and I'm trying to write a basic calculator.

However, in the code below, when I input any of the values, it prints all of the statements in the if block. I get the feeling that whatever I'm writing in is matching all of the conditions, hence printing all of the statements, but I can't see what I'm doing wrong.

import java.util.Scanner;

public class calculate {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //variable to store user input
    String userinputOperation;
    int userinputNumber;
    int userinputNumber2; 
    int result;

    //user input variables
    Scanner myscanner = new Scanner(System.in);
    Scanner number1 = new Scanner(System.in);
    Scanner number2 = new Scanner(System.in);

    //different options for calculator i.e., add, subtract, divide, multiply.
    String one = "Add"; // addition
    String two = "Subtract"; // subtract
    String three = "Multiply"; // multiplication
    String four = "Divide"; // division
    String five = "Exit the application."; // exit

    //explain how to use application
    System.out.print("Welcome to Barney's Calculator.");
    System.out.println(" What would you like to do?");
    System.out.println("Write 'one' to add two numbers.");
    System.out.println("Write 'two' to subtract two numbers.");
    System.out.println("Write 'three' to multiply two numbers together.");
    System.out.println("Write 'four' to divide two numbers.");
    System.out.println("Write 'five' to exit.");



    //obtain user input
    userinputOperation = myscanner.nextLine();

    //explain what user has selected
    if (userinputOperation.equals("one")); //add
    System.out.print("You have chosen to " + one + " two numbers");

    if (userinputOperation.equals("two")); //subtract
    System.out.print("You have chosen to " + two + " two numbers");

    if (userinputOperation.equals("three")); //multiply
    System.out.print("You have chosen to " + three + " two numbers");

    if (userinputOperation.equals("four")); //divide
    System.out.print("You have chosen to " + four + " two numbers");

    if (userinputOperation.equals("five")); //exit
    System.out.print("You have chosen to " + one);

    //obtain what the numbers the user wants to operate with

    System.out.println("Input the first number you want to operate with: ");
    userinputNumber = number1.nextInt();

    System.out.println("Input the second number you want to operate with: ");
    userinputNumber2 = number2.nextInt();

    //calculate stuff

    if (userinputOperation == "one"); //add the numbers
    result=(userinputNumber + userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "two"); //subtract the numbers
    result=(userinputNumber - userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "three"); //multiply
    result=(userinputNumber * userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "four"); //add
    result=(userinputNumber / userinputNumber2);
    System.out.print(result);


}

}

Any help would be greatly appreciated.

Barney G
  • 109
  • 1
  • 3
  • 10
  • Note the `;` after the `if` condition. What do you think that does? – Sotirios Delimanolis Oct 25 '13 at 13:15
  • possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) Why do you use `equals` some times and `==` others? – Sotirios Delimanolis Oct 25 '13 at 13:16
  • Thank you very much! Regarding the edit; Just bad practice at the moment, I've just been trying things out. I'll go over it to make it more uniform when I'm finished. – Barney G Oct 25 '13 at 13:17

2 Answers2

0

you have ; in every if statements like

if (userinputOperation.equals("one")); //add

If you keep ; then it means that end of statement.So even ifuserinputOperation.equals("one")); returns false this will be executedSystem.out.print("You have chosen to " + one + " two numbers"); similarly remove every ; and the end of if statements

final code is as follows

import java.util.Scanner;

public class calculate {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    //variable to store user input
    String userinputOperation;
    int userinputNumber;
    int userinputNumber2; 
    int result;

    //user input variables
    Scanner myscanner = new Scanner(System.in);
    Scanner number1 = new Scanner(System.in);
    Scanner number2 = new Scanner(System.in);

    //different options for calculator i.e., add, subtract, divide, multiply.
    String one = "Add"; // addition
    String two = "Subtract"; // subtract
    String three = "Multiply"; // multiplication
    String four = "Divide"; // division
    String five = "Exit the application."; // exit

    //explain how to use application
    System.out.print("Welcome to Barney's Calculator.");
    System.out.println(" What would you like to do?");
    System.out.println("Write 'one' to add two numbers.");
    System.out.println("Write 'two' to subtract two numbers.");
    System.out.println("Write 'three' to multiply two numbers together.");
    System.out.println("Write 'four' to divide two numbers.");
    System.out.println("Write 'five' to exit.");



    //obtain user input
    userinputOperation = myscanner.nextLine();

    //explain what user has selected
    if (userinputOperation.equals("one")) //add
    System.out.print("You have chosen to " + one + " two numbers");

    if (userinputOperation.equals("two")) //subtract
    System.out.print("You have chosen to " + two + " two numbers");

    if (userinputOperation.equals("three")) //multiply
    System.out.print("You have chosen to " + three + " two numbers");

    if (userinputOperation.equals("four")) //divide
    System.out.print("You have chosen to " + four + " two numbers");

    if (userinputOperation.equals("five")) //exit
    System.out.print("You have chosen to " + one);

    //obtain what the numbers the user wants to operate with

    System.out.println("Input the first number you want to operate with: ");
    userinputNumber = number1.nextInt();

    System.out.println("Input the second number you want to operate with: ");
    userinputNumber2 = number2.nextInt();

    //calculate stuff

    if (userinputOperation == "one") //add the numbers
    result=(userinputNumber + userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "two") //subtract the numbers
    result=(userinputNumber - userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "three") //multiply
    result=(userinputNumber * userinputNumber2);
    System.out.print(result);

    if (userinputOperation == "four") //add
    result=(userinputNumber / userinputNumber2);
    System.out.print(result);


}

}
SpringLearner
  • 13,738
  • 20
  • 78
  • 116
0

Remove the ; at the end of your statements. If you do not add a {} after the statement, it will only put the next statement in the if block. In this case, you have an empty statement, a semicolon.

Turn this

if (userinputOperation.equals("one"));
System.out.print("You have chosen to " + one + " two numbers");

into this

if (userinputOperation.equals("one"))
System.out.print("You have chosen to " + one + " two numbers");

or even better, because it makes it clearer, add brackets

if (userinputOperation.equals("one")){
    System.out.print("You have chosen to " + one + " two numbers");
}
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64