Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: If, else if, else statement not behaving as expected

  1. #1
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Question If, else if, else statement not behaving as expected

    I'm new to java, so if it's stupid simple, forgive me. I'm trying to parse commandline args using a giant if-else if-else block. Right now, i'm just trying to get it working by displaying the "help" block (tested works fine). Whenever I run the program, it executes the else segment...

    public static void main(String[] args) {
    		for (String s: args){
    			//DebugCode
    			System.out.println(s);
    			System.out.println(args[0]);
    		}
    		if (args.length < 1){
    			interactive();
    		}
    		else{
    			try{
    				//TODO fix
    				String dummy;
    				String command = String.valueOf(args[0]);
     
    				if (command == "-h" || command == "--help"){
    					help();
    				}else if (command == "-p" || command == "--push"){
    					//push	
    					dummy = "dummy";
    				}else{
    					//Invaled option
    					System.out.println("Unknown operating mode. Please restart the program");
    				}
    			}
    			catch(Exception ex){
    				System.out.println("Error:   " + ex.getStackTrace() + ex.getMessage());
    			}
    			finally{
     
    			}
    		}
     
    	}

    running the program with the -h CLI arg in eclipse prints -h in the first two debuglines, but as soon as it gets to the if-else block, it executes the else...any suggestions? I've also tried args[0] inplace of command


  2. #2
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If, else if, else statement not behaving as expected

    i've also tried elimating the
    ... || command == "--help"
    segment, but the problem persists

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: If, else if, else statement not behaving as expected

    Read the following and all will become clear.
    http://www.javaprogrammingforums.com...html#post18725
    Come back if you're still stuck
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. The Following User Says Thank You to newbie For This Useful Post:

    techwiz24 (April 17th, 2011)

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: If, else if, else statement not behaving as expected

    Quote Originally Posted by newbie View Post
    Read the following and all will become clear.
    http://www.javaprogrammingforums.com...html#post18725
    Come back if you're still stuck
    newbie is correct, you have made a simple error here. Please read the link provided.

    I have also moved this thread to Loops & Control Statements
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #5
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: If, else if, else statement not behaving as expected

    Ah. Thank you! It's weird that the == operator doesn't work, but the .equals() method does. I come from basic VB.net and python , I guess I was trying to do it how I would do it there lol. Thanks for the tip!

    On a side note, is there a reason why this doesn't work?

  7. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: If, else if, else statement not behaving as expected

    From the article:

    Quote Originally Posted by Common Java Mistakes
    The equals operator is used exclusively to compare the value of primitives, or to compare the references (memory addresses) of objects.

    The equals() method is used to define when two object values are equal. They do not necessarily have to be the same object (though, if they are they should return true for equals() as well as ==).
    To add a little more to this, most objects in Java are referenced by their memory address. When you use the == operator, you're comparing the addresses of two objects. So if you have two distinct object which happen to have the same value, would still have different memory addresses, and == would return false. If you want to test if two objects just happen to have the same value, you use the .equals() method.

    Note that for primitives (int, boolean, byte, float, etc.) there's no need for a .equals() method because these don't contain a reference to the value, but rather contain the value itself. However, if you're using wrapper classes (Integer, Float, Boolean, etc.), you must use the .equals() method because now you're holding references to the wrapper object, not the direct value.

    .equals() is used because Java doesn't have operator overloading. Python does, so you could overload == to do what the .equals() method does in Java. To allow Python users to still compare memory addresses, Python has the is keyword, which is analogous to Java's == operator.

    I've never used VB, so I couldn't say what the equivalent VB code would be
    Last edited by helloworld922; April 15th, 2011 at 12:13 PM.

Similar Threads

  1. [SOLVED] Output isn't as expected.
    By woodcutterni in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 12th, 2011, 11:55 AM
  2. Identifier expected
    By Sphinx in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 30th, 2010, 02:50 PM
  3. <identifier> expected
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 09:33 PM
  4. ';' expected?
    By noobish in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 21st, 2009, 11:55 AM
  5. [SOLVED] Java error "Another <identifier> expected"
    By bruint in forum What's Wrong With My Code?
    Replies: 23
    Last Post: May 1st, 2009, 08:47 AM