• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayIndexOutofBoundxception while printing arguments to Main

 
shreehari Gopalakrishnan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
shreehari Gopalakrishnan
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks greg


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

Thanks all
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic