my dog learned polymorphism*
The moose likes Beginning Java and the fly likes ArrayIndexOutofBoundxception while printing arguments to Main Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayIndexOutofBoundxception while printing arguments to Main" Watch "ArrayIndexOutofBoundxception while printing arguments to Main" New topic
Author

ArrayIndexOutofBoundxception while printing arguments to Main

shreehari Gopalakrishnan
Ranch Hand

Joined: Jun 01, 2004
Posts: 30
Hi Members

I found the code
class test
{
public static void main(String args[])
{
System.out.println(args[0]+args[1]);
}
}

is giving a runtime exception ( ArrayIndexOutofBound )

I am thinking why

Please answer it
Greg T Robertson
Ranch Hand

Joined: Nov 18, 2003
Posts: 91
Well perhaps you did not pass any command line arguments to the main? Do a System.out.println(args.length); first - I suspect that it will print a 0. This does not mean args[0] is there it means no arguments are there. A 1 will mean that args[0] is present, and 2 will mean args[1] is present. To pass command line arguments run as such: java test arg0 arg1 arg2 ...

hope this helps
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
Remember that unlike in some other languages the args you get passed into main include ONLY the arguments you type to the application and NOT the rest of the commandline.

For example, in some languages "myapp par1 par2 par3" would cause argv (args.length) to be 4 or 5, in Java "java MyApp par1 par2 par3" it would be 3.


42
shreehari Gopalakrishnan
Ranch Hand

Joined: Jun 01, 2004
Posts: 30
Thanks greg


Actually i have passed one argument in command line and printed args[1]
sorry paddon my careless ness

Thanks all
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: ArrayIndexOutofBoundxception while printing arguments to Main