-3

I'm java beginner and I'm learning about switch statement. I understand how switch statement works but when I try to convert from switch to if/else I got wrong answer. And I couldn't get the problem?

This is the switch statement

switch (y)
{
    case '+':
    case '-':
        checkPrecedence(y, 1);
        break;
    case '*':
    case '/':
        checkPrecedence(y, 2);
        break;
    case '(':
        opStack.push(y);
        break;
    case ')':
        checkBrackets();
        break;
    default:
        output = output + y;
        break;

my if/else statment

if (y == '+' || y == '_') {
    checkPrecedence(y, 1);
}
else if (y == '*' || y == '/') {
    checkPrecedence(y, 2);
}
else if (y == '(') {
    opStack.push(y);
}
else if (y == ')') {
    checkBrackets();
}
output = output + y;
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • What type is y? String? int? – Lexi Jun 30 '16 at 18:40
  • 2
    you got the wrong answer? what does that mean? it doesn't compile, it doesn't run, or your prof told you to try again...? for starters, your `switch` statement has a `default`, but your `if` seems to be missing its `else`. you are also comparing against `-` in the switch, but that seems to have typoed into a `_` in your if. – trooper Jun 30 '16 at 18:42
  • why do some of your case statements not have breaks? each case should have a break; Also your last else if statement should probably be an else statement, which is executed if all statements before it are false. – Radmation Jun 30 '16 at 18:43
  • You have to keep in mind that the `default` statement is not always run in a switch statement. When you look at your `if/else` statements that is not the case. – Josh Mein Jun 30 '16 at 18:43
  • 2
    @Radmation Every case does not require a break. They can fall through if they require the same processing. – Josh Mein Jun 30 '16 at 18:44
  • @JoshMein I learn something new everyday!! Thanks! That will take some getting use to. Can I combine statements in the case? Example `case 'x' || 'z':` – Radmation Jun 30 '16 at 18:45
  • @Radmation I dont believe so. – Josh Mein Jun 30 '16 at 18:46
  • 4
    You have a typo in the first if; underscore instead of minus sign. – Mick Mnemonic Jun 30 '16 at 18:47

2 Answers2

2

You forgot the last else:

 if(y=='+'||y=='-'){
  checkPrecedence(y,1);
  }
 else if(y=='*'||y=='/'){
  checkPrecedence(y,2);
 }
 else if(y=='('){
  opStack.push(y);
  }
 else if(y==')'){
     checkBrackets();
 } else {
     output=output+y;
 }

EDIT

and made the mentioned(by Eagle-Eye Mick Mnemonic) typo with the minus-sign

Turo
  • 4,724
  • 2
  • 14
  • 27
  • If you are going to just give them the answer, please at least explain why that is the case. – Josh Mein Jun 30 '16 at 18:46
  • I thought the difference to the default option was too obvious if i point on it – Turo Jun 30 '16 at 18:54
  • @Turo Agreed. The bug is too simple to explain, to the point where if you were to try to explain it, you would only end up explaining the general functionality of control statements. – nasukkin Jun 30 '16 at 19:10
-3

If String use String.equals to compare

if(y.equals("+") || y.equals("_")) {
   checkPrecedence(y,1);
}

This is probably your error. Assuming you are not using an int or char that is...Would help if we knew the datatype

Yusuf Jama
  • 123
  • 13
  • 1
    unlikely to be the problem. those single quotes imply that y is a `char`. http://stackoverflow.com/questions/439485/is-there-a-difference-between-single-and-double-quotes-in-java – trooper Jun 30 '16 at 18:52
  • i noticed that since he used in a switch...but it's nice to know what the datatype is.. notice how i said If String at first... – Yusuf Jama Jun 30 '16 at 18:55
  • If `y` in either `switch(y)` or `if (y =='+')` would be String, then OP wouldn't be able to compile `case '+'` nor `=='+'`. It must be `char` (or Character, or other numeric type which can be cast to char) but since it can't be String I don't see any point in posting answer about it. – Pshemo Jul 01 '16 at 01:51
  • did i see the datatype? he said switch so of course it's going to be char/int...but notice how i said "IF STRING" – Yusuf Jama Jul 04 '16 at 15:25