• Post Reply Bookmark Topic Watch Topic
  • New Topic

Ternary operator  RSS feed

 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a thought after once again being presented with the classic and annoying fizzbuzz question during an interview.  On the way home, I wondered about rewriting the solution using the ternary operator. 

I tried something like:



But, I get an error in the IDE (NetBeans) stating the line that contains  "i % (fizz * buzz) == 0 ? System.out.println("FizzBuzz") :" is not a statement.

What am I missing?  I have used the ternary operator in a similar manner before and am not sure what I am doing wrong here.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The ternary operator can't have any of its parts as void. The System.out.println() method returns is void, i.e. it does not return any value, hence the error.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You!
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getting closer,

I changed it to this:



New error is "unexpected type, required variable found value.  Am I trying to be too clever?  Is this something that can be done with the ternary operator or am I resigned to using the series of if else statements?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Depends on how you define "clever" -- I don't happen to think that writing code that people can't understand is particularly clever, much less code that doesn't work. Keep it simple and make it work.
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't write any assignments inside the ternary operator.
I would write
I wouldn't write First, it doesn't even complie.
Second, it just looks wrong.
 
Tobias Bachert
Ranch Hand
Posts: 86
18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use the result of the ternary operator (assign it to a variable/return it/invoke a method with it/etc.).
(JLS §15.25: "In fact, by the grammar of expression statements (§14.8), it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear.")

Besides that I would allign the code a bit different:

(You are missing parentheses around fizz * buzz in your last version.)
 
Zachary Griggs
Ranch Hand
Posts: 83
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clearly this is the most efficient way of writing fizzbuzz  : https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Everybody knows that indenting nested ?: operations is difficult, but why are you aligning fizz and buzz differently in lines 5 and 6? Why are you using & not && in line 4?

I would probably prefer String#valueOf to Integer#toString.
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for all of your comments and suggestions.  I had a thought just before dozing off last night and had to grab my computer.  This was the result



This code compiles and works! 

Thanks again for your suggestions!
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That solution looks like it prints the value of i unconditionally, then fizz, buzz, or fizzbuzz. Isn't the original requirement to conditionally print fizz, buzz, or fizzbuzz instead of the number?
 
Paul Peterson
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:That solution looks like it prints the value of i unconditionally, then fizz, buzz, or fizzbuzz. Isn't the original requirement to conditionally print fizz, buzz, or fizzbuzz instead of the number?


I've seen the question written three different ways.  One includes the number every time, the way I wrote this one.

only display the number if the i is not divisible by 3, 5, or 15


or display fizz if i is divisible by 3 or ends with 3

 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!