• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

for loop

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Guys can anyone explain in breif about how does the following example gives a run time error?




class JMM101 {
public static void main(String[] args) {
int i = 0;
while (i++ < args.length) {
System.out.print(args[i]);
}}}


java JMM101 A B C D E F



Please Help!
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You first check should be to check the error. I can guess what it is, but you haven't provided it
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other things which may be useful:
* what is the output of the program, compared to what you expect?
* What does it output if you add a line like this:
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi aafreen,

I think by now u might have got the answer , if u r still confused then
have a look on this code

class JMM101
{
public static void main(String[] args) {

int i = 0;

while (i++ < args.length )
{
System.out.print("I=" + i );
System.out.println(args[i]);
}

}
}


The exception itself explains the prob .


I=0A
I=1B
I=2C
I=3D
I=4E
I=5F
I=6Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at JMM101.main(JMM101.java:11)




Hope , answered u
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Situations like this are why (IMO of course) it unnecessarily complicates code when side-effects are added to conditional statments. I try to avoid this whenever possible. I even avoid the (admittedly less confusing than this) widespread idiom for reading files
Its worth it to me to write the few extra lines to make the code clearer. I especially don't like mixing increment and decrement operations with anything else. That just leads to wierd situations that (judging from the posts I read on that forum) they seem to like to write SCJP exam questions about.
 
Aafreen Moinuddin
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Phew! That was easy .. Thanks Faisal
reply
    Bookmark Topic Watch Topic
  • New Topic