Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

crtvmac

Guest
Original poster
Aug 14, 2009
85
0
Will someone tell me the error in my programming please? The output I get when I run the program below is: args.length = 0. I also get ArrayIndexOutOfBoundsException in argument two.


Code:
public class CommandLine {
  public static void main(String args[ ]) {
     System.out.println("args.length = " + args.length);
     System.out.println("args[0] = " + args[0]);
     System.out.println("args[1] = " + args[1]);
     System.out.println("args[2] = " + args[2]);
    }
}


Shouldn't the output be ?
args.length = 3
arg.[0] = 1
arg.[1] = 2
arg.[2] = 3
 
Please use the
Code:
 tag to mark your code for easier readability.

Do you understand what your code is supposed to do?
 
How many arguments did you pass to the program when you ran it? Did you pass "1 2 3"? If not, what did you pass?

-Lee
 
The elements in the 'args' array depends on what you type into the command line when you try to run the program. For instance:

Code:
java CommandLine
will give you an args array of length 0.

Code:
java CommandLine alpha bravo charlie
will give you an args array length of 3.
 
Please use the
Code:
 tag to mark your code for easier readability.

Do you understand what your code is supposed to do?[/QUOTE]

No. I don't know what a [CODE] tag is.

I must not. Thats way I ask the question.
 
How many arguments did you pass to the program when you ran it? Did you pass "1 2 3"? If not, what did you pass?

-Lee

I show you the program. Thats is as far as I understand the program. Thats is way I displayed the program and ask for help. Any other information about this program, I don't understand.
 
I ran the program using BlueJ complier. Is there a better way?

Some IDE (Integrated Development Environments) don't make it easy to pass arguments into your "main" method.

Do you know how to run it from the command line?

If you're using a Mac, open the Terminal app, find your .class files and type:

Code:
java CommandLine arg1 arg2 arg3

the three tokens after CommandLine will be what is populated into your 'args' array.
 
Java Programming

"Thank you all for assisting me, by answering my question with a question."
 
No. I don't know what a
Code:
 tag is.

I must not. Thats way I ask the question.[/QUOTE]

You already used it, so I guess you know what it is now.  Asking the question doesn't indicate you don't understand what the program is supposed to do.

[quote="crtvmac, post: 9447675"]I ran the program using BlueJ complier. Is there a better way?[/QUOTE]

That is probably why there are no arguments.

[quote="crtvmac, post: 9447665"]I show you the program. Thats is as far as I understand the program. Thats is way I displayed the program and ask for help. Any other information about this program, I don't understand.[/QUOTE]

[quote="crtvmac, post: 9447713"]All I know about the program is what you see. Blink[/QUOTE]

Ok, did you copy it from somewhere? are you trying to understand a random piece of code? Why are you running it if you don't know/care what it does?  :confused:

Or are you being mysterious?  ;)
 
"Thank you all for assisting me, by answering my question with a question."

The questions are actually all legit, because most of the info is needed to understand what you might be doing wrong.

They are also trying to help you understand the program. Teaching you how to fish kinda thing. :eek:
 
I ran the program using BlueJ complier. Is there a better way?

The BlueJ website tells you how to pass command-line args to your main class:

http://www.bluej.org/help/archive.html#tip9

I recommend copying and pasting the text from the web-page. It appears to be custom-made for your CommandLine class.


The above link came from BlueJ FAQs:
http://www.bluej.org/help/faq.html#main-args

Which I found by typing bluej command line into google.


The simple answer to what's wrong with your program is it blindly assumes the array it receives has 3 elements. This may not always be true, since the array passed to main() represents command-line parameters entered on the command-line.

If any method receives an array parameter and doesn't already know the length of the array, it should be written to use the actual length of the received array. This is because the receiver (i.e. the method that was called) doesn't get to choose the length. The sender (i.e. the point where the target method was called) is the one who chooses what the receiver receives.

If you entered that program from a book or tutorial, it should have told you how to run it. Since you haven't said what book or tutorial you're working from, if any, no one can offer you better advice. You have to tell us what you're working from. The program didn't write itself.
 
crtvmac: You are assuming we're trying to give you a hard time, etc. What we're really trying to do is shed light on why you got the behavior you got. Essentially, this program:
prints "args.length = ", then displays the number of command line arguments passed to the program. if none are passed, this will display 0.

next, the program prints "args[0] = ", then assumes there is at least one arguments, and prints the first argument.

next, the program prints "args[1] = ", then assumes there are at least two arguments, and prints the second argument.

next, the program prints "args[2] = ", then assumes there are at least three arguments, and prints the third argument.

If you don't pass any arguments, then the assumptions for everything but the first print are incorrect, so you'll get an error (in this case, an exception, a special sort of error) like the one you experienced.

This (found by googling "BlueJ arguments") explains how to set the arguments to main (normally passed on the command line):
http://www.javakb.com/Uwe/Forum.aspx/java-programmer/140/BlueJ-Arguments

-Lee

Edit: Also, one should never assume any non-primitive (array,object, etc.) is non-null. I don't know if there are platforms where args passed to main are null instead of length 0, but in general, checking things for null before using .length is best.
 
Also, you should practice "defensive coding." Never assume an array has a particular length, then blindly try to access an element at a particular index. In other words, before accessing the 2nd index, make sure it's really there.

Instead of this...
Code:
System.out.println(args[2]);

You should do this...
Code:
if (args.length >= 3)
  System.out.println(args[2]);

Just a friendly tip from someone who's been coding for 20 years. (Whoa that's a depressing thought).
 
The code works (bold text is stuff I typed):
Code:
[B]vi CommandLine.java
javac CommandLine.java 
java -cp . CommandLine 1 2 3[/B]
args.length = 3
args[0] = 1
args[1] = 2
args[2] = 3
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.