The Artima Developer Community
Sponsored Link

Java Answers Forum
Comparing Command-Line Arguments

3 replies on 1 page. Most recent reply: Nov 21, 2005 12:21 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Joe Spoot

Posts: 4
Nickname: sllobb
Registered: Nov, 2005

Comparing Command-Line Arguments Posted: Nov 18, 2005 9:26 AM
Reply to this message Reply
Advertisement
Ok, I want to usa a command-Line Argument in an if statement. However, when I write code such as:

if(args[0] == "-t")

it is always skipped. I realize now that it is comparing the argument's reference to -t. How do I make it compare the value?


Dave Hinton

Posts: 42
Nickname: catbells
Registered: Oct, 2003

Re: Comparing Command-Line Arguments Posted: Nov 19, 2005 9:36 AM
Reply to this message Reply
Use equals(), e.g.,
if (args[0].equals("-t"))

java-samples.com Site

Posts: 1
Nickname: javasamp
Registered: Nov, 2005

Re: Comparing Command-Line Arguments Posted: Nov 20, 2005 6:25 AM
Reply to this message Reply
Its always better to check if args[0] in not null before comparing its value.

Eg.
if(args[0] != null && args[0].equals("-t"))

Failing to do so may throw a null pointer exception if the value is null.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Comparing Command-Line Arguments Posted: Nov 21, 2005 12:21 AM
Reply to this message Reply
'==' works only for primitive types.
If you use it for Objects, not the content will be compared, but the 'pointer' (C++ expression), the position in RAM will be compared.

Flat View: This topic has 3 replies on 1 page
Topic: Running an Excel program from Java Previous Topic   Next Topic Topic: Protected method in an Interface

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use