Results 1 to 5 of 5
  1. #1
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default 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");
    	}
    }

  2. #2
    joet1984 is offline Member
    Join Date
    Jun 2011
    Posts
    11
    Rep Power
    0

    Default

    And sorry about the misleading processData() method name forgot to change it

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    it doesn't compile anyone know why?
    Almost certainly because of compiler errors. Perhaps you could say what they are.

    -----

    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.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,404
    Rep Power
    20

    Default

    ... and even if it did (which it doesn't, as already said), an if statement requires a boolean expression.

    db

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. why i can not compile? please help
    By zypchun in forum New To Java
    Replies: 4
    Last Post: 09-29-2013, 10:11 PM
  2. would this compile?
    By stringkilla in forum New To Java
    Replies: 10
    Last Post: 10-24-2010, 03:27 PM
  3. Can't Compile
    By sidk47 in forum JavaServer Pages (JSP) and JSTL
    Replies: 7
    Last Post: 06-15-2010, 04:43 PM
  4. Help with compile
    By mr_anderson in forum NetBeans
    Replies: 7
    Last Post: 06-10-2010, 04:03 AM
  5. Not able to compile
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-09-2008, 10:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •