Skip to main content
gamedev.net
🔒 Locked

[java] Alternate IF statement

Started by Jonbca 7 replies 450+ views
Original Post
Jonbca
Jonbca
ok, I''m making a database for my programming class and I want to be able to check a variable using the ? method in c++ but I don''t know how to do it. Actually, I''m not even sure you''re aloud to. The syntex in c++ is:
  
arg1 = arg2 ? returnSomeThing: orSomeThingElse
  
I think I''ve seen it in a java program somewhere, but I''m not sure. ANy help would be appreciated.
WayfarerX
WayfarerX
Works just like C++.

Object value = getSomeValue();
String result = value == null ? "value-is-null" : "value-is-not-null";


[edited by - wayfarerx on October 1, 2002 4:46:10 PM]
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Jonbca
Jonbca
I tried to do something pretty similar to that. I''m going

state[4]==1 ? search.Show(true):search.Show(false);

but I''m getting an invalid assignment error on the ==

???
dede
dede
= is the assignment operator

== is the boolean operator
WayfarerX
WayfarerX
From http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293
quote:
Note that it is not permitted for either the second or the third operand expression to be an invocation of a void method. In fact, it is not permitted for a conditional expression to appear in any context where an invocation of a void method could appear (§14.8).

Is that what you're doing? Also, wouldn't this be a little more concise in your scenerio?
search.Show(state[4] == 1);  


[edited by - wayfarerx on October 1, 2002 5:04:06 PM]
"There is no reason good should not triumph at least as often as evil. The triumph of anything is a matter of organization. If there are such things as angels, I hope that they're organized along the lines of the mafia." -Kurt Vonnegut
Jonbca
Jonbca
thanks a lot. Both of my methods were a void return so that way of doing it wouldn''t work. Thanks for the

search.Show(state[4]==1)

idea. Wow, can you say blond moment, gotta love missing something that easy.
Viro
Viro
quote:
Original post by Anonymous Poster
I hate this syntax....it''s obfuscated and JAVA would be a better (cleaner) language without it. I don''t find much other cryptic stuff in JAVA, so why this sh**?


What so obfuscated about that? Besides, if you don''t like it, you could do something else.

if(state[4]==1)
search.show(true);
else
search.show(false);


I suppose its a matter of preference. I''d still prefer the original syntax because it is more concise.
"Linux is not about free software, it is about community," -- Steve Balmer, Microsoft Chief Executive.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.