Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java arguments

Posted on 2007-03-20
4
Medium Priority
?
248 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 260 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 240 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses

721 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