• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Ternary operator cannot get it to work

 
Tanya Ruttenberg
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can not get the ternary operator to work.

What is wrong with this code?



I'm getting these compile errors from eclipse.

Multiple markers at this line
- Syntax error on token "?", ; expected
- Syntax error on token ":", ; expected


But of course if I put in the semi-colons, it does not help:

Multiple markers at this line
- Syntax error on token ":", invalid EmptyStatement
- Syntax error on token "?", delete this token



I have this same problem every time I try to use the ternary operator. What the heck am I doing wrong?

Thank you in advance.

TDR
 
Carey Brown
Ranch Hand
Posts: 1392
16
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The expressions to the right of '?' and ':' must evaluate to data of some type. Neither 'throw' nor 'return' meets this qualification.
 
Rob Spoor
Sheriff
Pie
Posts: 20512
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. You can't put statements in the ternary operator, only values. These values may be the results of method calls but they must still be values. That means you can't put a System.out.println() or a throw statement there.

I'm afraid you will need to use a normal if-statement here:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic