Using a String in run configurations as an argument and using it in an if-statement

The code below works as long as the argument in the run configuration equals "-output". But when the arguments are empty the compiler throws and ArrayOutOfBoundsException.

The point of this piece of code would eventually be to; - Perform an action when -output is written in the run configurations arguments - Perform something else if the arguments are empty or different from -output

I found many problems that looked like this one. But I've been working on a solutions for far to long, so I started a new post. Help is very much appreciated.

...
public static void main(String[] args) {
        Version_5 v5 = new Version_5("Test");
        {
            if(args[0].equals("-output")){
                System.out.println("It works");
            }
        }
    }
...

Answers


You need to check if you have arguments first, that's all.

if ((args.length > 0) && (args[0].equals("-output")) {
    ...

You might also consider using an argument-parsing library, of which there are several.

What are the extra brackets for?


As you can see String args[] is an array with a specific size. If you don't pass an argument the size is zero. Before you check what is at args[0] check if args has a size with args.length.

if (args.length>0){
   //do something
}
else if (args[0].equals("-output")){
   System.out.println("It works");
}

Need Your Help

CSS: centering a tooltip for a button horizontally?

css navigation tooltip

I'm just not able to find a solution to this.

Error during insert the XML file content into an access database

asp.net database vb.net insert

in a web application, i want to insert the XML file content into an access database, i got the code (below), but there is an error with this line

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.