Solved

Java arguments

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Restore security prompts not working 10 261
Print Rhino Java Array in Javascript 1 58
Java array 10 87
maximize the sum of fractions 33 75
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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:
Suggested Courses

738 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