Solved

Java arguments

Posted on 2007-03-20
4
233 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

895 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

11 Experts available now in Live!

Get 1:1 Help Now