Solved

Java arguments

Posted on 2007-03-20
4
239 Views
Last Modified: 2013-11-23
My program accepts 2 arguments. But if the arguments are not present, when the class is run from prompt, I want to display a message to the user

So I am doing

public static void main(String[] args) {

            if(args[0]==null || args[1]==null) {
                  
                  System.out.println("Usage: java [-classpath] MyClass source target");

            }
            String sourcePath = args[0];      
            String targetPath = args[1];
       
            
}

But the problem is when any of the arguments is not present, I get ArrayOutofBoundsException. Please help

0
Comment
Question by:odumbe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:SamsonChung
ID: 18755694
use args.length()

if it is < 2, System.out.println...
0
 
LVL 6

Accepted Solution

by:
SamsonChung earned 65 total points
ID: 18756309
the reason for array out of bounds is because you are attempting to look at args[0] and args[1]

this would require that value to exists before you can do that comparison.

However, args[] is currently empty. therefore args[1] does not exists.

if you used my suggestion, you'd end up with an integer that would be the lenght of the array.

Now we know that you want atleast 2 things in the array before you would let the system continue. so we check for the length of the array. and that is sufficient to allow our code to continue.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18757178
Yes, you should be using:

if ( args[0] == null || args[1] == null )
                  System.out.println("Usage: java [-classpath] MyClass source target");
else if ( args.length == 2 )
{
            String sourcePath = args[0];      
            String targetPath = args[1];
}
0
 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 60 total points
ID: 18757181
Sorry:

if ( args.length < 2 )
                  System.out.println("Usage: java [-classpath] MyClass source target");
else if ( args.length == 2 )
{
            String sourcePath = args[0];      
            String targetPath = args[1];
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

735 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