Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question related to Main method arg

 
Indravadan T Patel
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ls chin
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 225
Debian Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic