Results 1 to 5 of 5
Thread: why won't this compile
- 08-31-2011, 07:49 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
why won't this compile
I saw this challenge on another thread that has been closed and I'm a java beginner so I decided to try it. It's extremely ugly code but that is the point here to try to get this to work using the ? : operator. This is what I came up with and it doesn't compile anyone know why? (sorry that its a huge pain to read ;])
Java Code:import java.lang.*; class Test{ public static void main (String args[]){ String variable1 = args[0]; String variable2 = args[1]; String variable3 = args[2]; variable1.compareTo(variable2) ? (variable2.compareTo(variable3) ? System.out.println("all values equal") : System.out.println("2nd and 3rd not equal")) : processData(); } public void processData(){ System.out.println("1st and 2nd not equal"); } }
- 08-31-2011, 07:51 AM #2
Member
- Join Date
- Jun 2011
- Posts
- 11
- Rep Power
- 0
And sorry about the misleading processData() method name forgot to change it
- 08-31-2011, 08:20 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Almost certainly because of compiler errors. Perhaps you could say what they are.it doesn't compile anyone know why?
-----
You are quite correct about the code being ugly. So the correct answer is "don't write code like this". But if you want to persue it you will, sooner or later, have to look up exactly what it is that the ?: operator does. Hint: it does not create if-else statements.
- 08-31-2011, 08:56 AM #4
... and even if it did (which it doesn't, as already said), an if statement requires a boolean expression.
db
- 08-31-2011, 09:50 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
... that is how you should use the ternary operator.
Java Code:result = someCondition ? value1 : value2;
Similar Threads
-
why i can not compile? please help
By zypchun in forum New To JavaReplies: 3Last Post: 04-20-2011, 05:48 PM -
would this compile?
By stringkilla in forum New To JavaReplies: 10Last Post: 10-24-2010, 03:27 PM -
Can't Compile
By sidk47 in forum JavaServer Pages (JSP) and JSTLReplies: 7Last Post: 06-15-2010, 04:43 PM -
Help with compile
By mr_anderson in forum NetBeansReplies: 7Last Post: 06-10-2010, 04:03 AM -
Not able to compile
By bugger in forum New To JavaReplies: 2Last Post: 01-09-2008, 10:13 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks