This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Ternary operator cannot get it to work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Ternary operator cannot get it to work" Watch "Ternary operator cannot get it to work" New topic
Author

Ternary operator cannot get it to work

Tanya Ruttenberg
Ranch Hand

Joined: Jun 22, 2009
Posts: 40
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

Joined: Nov 19, 2001
Posts: 173

The expressions to the right of '?' and ':' must evaluate to data of some type. Neither 'throw' nor 'return' meets this qualification.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19655
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Ternary operator cannot get it to work
 
Similar Threads
Problem with DOMConfigurator.configure("log4j.xml");
Error in using JWIG
DOMConfigurator.configure("log4j.xml") error
Parameterising in ArrayList throws error
Java SE 6 validating Required data elements