0

I'm just writing a basic code that takes 2 inputs and an operator and solves. I have to code the program to tell it to not let a division of 0 happen but I my else keeps having an error message saying that there is a syntax error token. I'm not sure why because I have done if else statements in the past and it doesn't look any different. I am new at programming. Help would be appreciated

 if((operator == '/') && (operand2 == 0))
   JOptionPane.showMessageDialog(null,"Division by 0 not allowed"); 
   System.exit(0);
 else 
  add = operand1 + operand2;
  mult = operand1 * operand2; 
  sub = operand1 - operand2;
  div = operand1 / operand2;
  remainder = operand1 % operand2;
3
  • docs.oracle.com/javase/tutorial/java/nutsandbolts/if.html Commented Feb 17, 2012 at 3:12
  • Where is your operator variable?? And you should put your if/else in a block for multiple statements. Commented Feb 17, 2012 at 3:13
  • Is this a homework question? If so, you should add the homework tag. Commented Feb 17, 2012 at 3:13

3 Answers 3

5

If you have more than one statement within a block, you need to surround it with braces:

if (...) {
  ...
} else {
  ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

thank you very much. I was messing around with those braces before I guess I missed it in the one place that was correct haha
1

You need to have curly braces { } around the if and the else if there are more than one line of code under them. This is the reason you are having the issue

Comments

0

As the others before me have stated, yes, you need to have curly braces surrounding your if statement bodys when they are more than one line. Java pretty much only sees this part of your code.

if((operator == '/') && (operand2 == 0))
  JOptionPane.showMessageDialog(null,"Division by 0 not allowed"); 
else 
  add = operand1 + operand2;

Now, however, if you were to add curly braces {} to your if and else blocks, Java would be able to read the whole code. It looks something like this

if((operator == '/') && (operand2 == 0))
{
 JOptionPane.showMessageDialog(null,"Division by 0 not allowed"); 
 System.exit(0);
}
else
{ 
 add = operand1 + operand2;
 mult = operand1 * operand2; 
 sub = operand1 - operand2;
 div = operand1 / operand2;
 remainder = operand1 % operand2;
}

1 Comment

Your first paragraph and code sample are wrong. What the compiler actually sees is if((operator == '/') && (operand2 == 0)) JOptionPane.showMessageDialog(null,"Division by 0 not allowed"); System.exit(0); else, and without the {} the syntax error is because of the else with no matching if to start the block. (The System.exit(0); is still seen, however.) IOW, the if block is closed with the end of the call to showMessageDialog() and it's terminating ;, leaving the call to exit() as a new single statement, followed by an orphaned else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.