Hexley21
Hexley21

Reputation: 664

Java both If and else statements are executed

I wrote a little console app in java, which is made of If and else statements.
And on the output, both of the right If and else statements are executed.
How is it possible to fix? what's the problem?

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);

    System.out.println("Enter a first number");
    int FirstNum = in.nextInt();

    System.out.println("Enter an Operator");
    String Operator = in.next();

    System.out.println("Enter a second number");
    int SecondNum = in.nextInt();

    try{
        if(Operator.equals("+")){
            System.out.println(FirstNum + SecondNum);
        }
        if (Operator.equals("-")){
            System.out.println(FirstNum - SecondNum);
        }
        if (Operator.equals("*")){
            System.out.println(FirstNum * SecondNum);
        }
        if (Operator.equals("/")) {
            System.out.println(FirstNum / SecondNum);
        }
        else {
            System.out.println("invalid Operator");
        }
    }
    catch (Exception e){
        System.out.println("Invalid Operator or number");
    }
}

}

Output example:

Enter a first number
1
Enter an Operator
+
Enter a second number
2
3
invalid Operator

Process finished with exit code 0

JDK: java 15

Upvotes: 1

Views: 196

Answers (3)

Abubakr
Abubakr

Reputation: 312

You should be using else if for the subsequent statements. If you just use if ... if ... if ... else, then essentially each if is evaluated separately and only the FINAL if else is put together as one or the other.

try{
    if(Operator.equals("+")){
        System.out.println(FirstNum + SecondNum);
    }
    else if (Operator.equals("-")){
        System.out.println(FirstNum - SecondNum);
    }
    else if (Operator.equals("*")){
        System.out.println(FirstNum * SecondNum);
    }
    else if (Operator.equals("/")) {
        System.out.println(FirstNum / SecondNum);
    }
    else {
        System.out.println("invalid Operator");
    }
}
catch (Exception e){
    System.out.println("Invalid Operator or number");
}

Upvotes: 2

J.F.
J.F.

Reputation: 15235

First of all: Is not possible execute both statements.

So, ¿what's happen? The structure if/else is only the last two lines. That is: If the operator is not / the code will execute the elsestatement.

You need an if/else if/else statement

if(Operator.equals("+")){
    System.out.println(FirstNum + SecondNum);
}
else if (Operator.equals("-")){
    System.out.println(FirstNum - SecondNum);
}
else if (Operator.equals("*")){
    System.out.println(FirstNum * SecondNum);
}
else if (Operator.equals("/")) {
    System.out.println(FirstNum / SecondNum);
}
else {
    System.out.println("invalid Operator");
}

Upvotes: 2

chrisbyte
chrisbyte

Reputation: 1633

When writing if statements like this, you need to chain them together with else if, basically an else for every if with a final else. What is happening is you are saying "if this. And if this. And if this" etc. It is testing the last if and since it is not a forward slash, you are getting into the else.

Try this:

    if(Operator.equals("+")){
        System.out.println(FirstNum + SecondNum);
    }
    else if (Operator.equals("-")){
        System.out.println(FirstNum - SecondNum);
    }
    else if (Operator.equals("*")){
        System.out.println(FirstNum * SecondNum);
    }
    else if (Operator.equals("/")) {
        System.out.println(FirstNum / SecondNum);
    }
    else {
        System.out.println("invalid Operator");
    }

Upvotes: 4

Related Questions