Solved

args not working in if statement

Posted on 2006-10-30
3
268 Views
Last Modified: 2012-05-05
I want to check when the user has entered in a question mark (or "/?" or "-?") as an argument.
This is my code:

if (args[0] == "/?" || args[0] == "-?" || args[0] == "?") {
   System.out.println("help stuff")
}

I passed "/?" as an argument and nothing happened. I added some debugging code:

System.out.println(args[0]);
if (args[0] == "/?" || args[0] == "-?" || args[0] == "?") {
   System.out.println("help stuff")
} else {
   System.out.println("this text shouldnt be printed");
}

Once again I pass "/?" as an argument and get back:
/?
this text shouldnt be printed

Please help me.
Thanks in advance.

0
Comment
Question by:snowy0
  • 3
3 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 17838097
>>if (args[0] == "/?" || args[0] == "-?" || args[0] == "?") {


should be


if ("//?".equals(args[0] ....................................................
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 17838099
Sorry

if ("/?".equals(args[0] ....................................................
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17840395
:-)

You could also do

if (args[0].matches("[/\\-]*\\?"))
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now