• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ternary operator cannot get it to work

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic