wood burning stoves*
The moose likes Beginning Java and the fly likes Question related to Main method arg Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question related to Main method arg " Watch "Question related to Main method arg " New topic
Author

Question related to Main method arg

Indravadan T Patel
Greenhorn

Joined: Sep 09, 2007
Posts: 29
Hi,

I am using the following code and get the out such like that. I am confuse with the output why that happened.



Output :
0
1
2
3
4

Regards,
Indravadan Patel

[edit]Add code tags. CR[/edit]
[ August 10, 2008: Message edited by: Campbell Ritchie ]
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

If you use e.printStackTrace() instead, it�s more obvious:
getMessage() on ArrayIndexOutOfBoundsException returns the index that didn�t exist in the array.
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
the only way you should get that output is by running the program as...



otherwise the index out of bounds is what i'd expect,

try
args.length rather than 5


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Ls chin
Ranch Hand

Joined: Jun 28, 2008
Posts: 99
Originally posted by Indravadan T Patel:
I am using the following code and get the out such like that. I am confuse with the output why that happened.
Hi there,
What type of output were you expecting? Were you expecting an exception to occur?

for (int i = 0; i < 5; i ++){
System.out.println(args[i]);
}

Without the 'try' and 'catch', the above code will print out exactly like you mentioned:-
0
1
2
3
4
[ August 06, 2008: Message edited by: LS chin ]
Indravadan T Patel
Greenhorn

Joined: Sep 09, 2007
Posts: 29
Originally posted by Bill Shirley:
the only way you should get that output is by running the program as...



otherwise the index out of bounds is what i'd expect,

try
args.length rather than 5


Hi,

I run that above program like : java Q4 and the above mentined output.

Regards,
Indravadan Patel
Indravadan T Patel
Greenhorn

Joined: Sep 09, 2007
Posts: 29
Originally posted by LS chin:
Hi there,
What type of output were you expecting? Were you expecting an exception to occur?

for (int i = 0; i < 5; i ++){
System.out.println(args[i]);
}

Without the 'try' and 'catch', the above code will print out exactly like you mentioned:-
0
1
2
3
4

[ August 06, 2008: Message edited by: LS chin ]


Hi,

With try and catch block, i get the output as I had mentioned and
for running the command line code is : java Q4.

And get the output as given above.


Regards,
Indravadan Patel
Carey Evans
Ranch Hand

Joined: May 27, 2008
Posts: 225

Each time through the loop, args[i] throws ArrayIndexOutOfBoundsException with the value of i as the message, so you're printing i each time. Try System.out.println(e) instead of e.getMessage().
 
wood burning stoves
 
subject: Question related to Main method arg