Solved

Java arguments

Posted on 2007-03-20
4
231 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
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:SamsonChung
Comment Utility
use args.length()

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

Accepted Solution

by:
SamsonChung earned 65 total points
Comment Utility
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:mayankeagle
Comment Utility
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:mayankeagle
mayankeagle earned 60 total points
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

771 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