1. Member
Join Date
Feb 2014
Posts
7
Rep Power
0

Multiple if Statements?

So I'm trying to make a simple calculator, just to see if I can:

Java Code:
```import java.util.Scanner;

class Calculator {

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

System.out.println("Hello, and welcome to Calculator.");
System.out.println("Please type M for multiplication, D for division, S for addition, or A for subtraction.");

System.out.println("You have selected multiplication. Please type the first number you wish to multiply.");

double multiplyOne = calculator.nextDouble();
double multiplyTwo = calculator.nextDouble();
double multiplyResult = multiplyOne * multiplyTwo;
System.out.print(multiplyResult);

calculator.close();
System.exit(0);
System.out.println("You have selected division. Please type the first number you wish to divide.");

double divideOne = calculator.nextDouble();
double divideTwo = calculator.nextDouble();
double divideResult = divideOne / divideTwo;
System.out.print(divideResult);

calculator.close();
System.exit(0);

calculator.close();
System.exit(0);

System.out.println("You have selected subtraction. Please type the first number you wish to subtract.");

double subtractOne = calculator.nextDouble();
double subtractTwo = calculator.nextDouble();
double subtractResult = subtractOne - subtractTwo;
System.out.print(subtractResult);

calculator.close();
System.exit(0);

}

}

}```
(I'm probably doing something really stupid) Anyway, for some reason whenever I type anything other than M, it still sends me to division, even though there's no else. What am I doing wrong here?

2. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,433
Rep Power
9

Re: Multiple if Statements?

Poorly placed ; in if statements.

Regards,
Jim

3. Member
Join Date
Feb 2014
Posts
7
Rep Power
0

Re: Multiple if Statements?

Thanks, I can't believe I didn't see that.

4. Re: Multiple if Statements?

Also putting a statement after a } is confusing.
It's better to start a new statement on a new line.
Also the ending } for a block should be in the same column as the first character on the line that starts the block. For the posted code, the } should be in the same column and below the i in if

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,433
Rep Power
9

Re: Multiple if Statements?

I just happened to spot it. I would be nice if the compiler could warn about such things that don't make obvious sense. I vaguely
remember the PL/1 compiler would warn about a semicolon in comments. And assuming you are new at programming you have redundant code that could be removed. You could do something like this (using pseudo code).

Java Code:
```if ( some test) {
} else if (another test) {
} else if (another test) {
} else (
}
Program will exit normally after last statement.

Regards,
Jim
Last edited by jim829; 08-30-2014 at 11:28 PM.

6. Re: Multiple if Statements?

The javac compiler issues a warning with the -Xlint option:
Java Code:
```Running: D:\Java\jdk1.7.0_45\bin\javac.exe -cp . -Xlint TestCode18.java

TestCode18.java:553: warning: [empty] empty statement after if
if( 1 > 2); {```

7. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
5,433
Rep Power
9

Re: Multiple if Statements?

Great! Did not know that.

Regards,
Jim

Posting Permissions

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