• 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
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Array!

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will the following give runtime exception?
int i=0;
while (i++ < args.length) {
System.out.print(args[i]);
}
Since the condition is checkd for i less than args.length, it won't..rite?Can anybody explain pl!Thanks
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does a simple main method containing that code do when you run it?
Have you tried it? Or try to mentally simulate the code with an array of one string.
[ May 11, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i dont think it should give an error
 
Sridhar Srinivasan
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the command line arguments passed is A B C D and the answer they have given is that it gives run time exception.It shouldn't be correct rite?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It should give Runtime Exception.
Reason:
At first iteration 0 < 4, and i = 1
At Second iteration 1 < 4, and 1 = 2
At Third iteration 2 < 4, and i = 3
At Fourth iteration 3 < 4, an i = 4
So in fourth iteration, it will try to get a value from array at location 4 where as the size of array is 4, hence runtime exception.
Hope I am clear.
Cheers~
Sharad
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The while loop checks i++ which returns the value of i and then increments it. So after the i == (args.length - 1) test returns true, i will equal args.length. Since you can only reference args[0] - args[args.length - 1], you will get an array out of bounds exception.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 int i=0;
2 while (i++ < args.length) {
3 System.out.print(args[i]);
4 }
In line 2 i has a value and in line 3 it has another.
So, you pass the test in the while, but you print the next element of args.
That's why you got a runtime exception.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
when written after the variable, the "++" operator fires AFTER the other stuff on the line is done. so we go around the loop a few times. eventually, we enter the loop with i equalling 3. the first thing that happens is we to the test. since args.length() is 4, we pass.
AFTER WE DO THAT TEST, we increment i to 4. you now try and get the element at index 4, which throws the exception.
You should always try running code like this when what the author says doesn't make sense. if there is an exception thrown, take out the line and try and figure out why. you could take out the "args[i]" and just print i. you'd see it printing values 1,2,3 and 4. then you can try and figure out why it's that instead of 0,1,2,3
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by fred rosenberger:

You should always try running code like this when what the author says doesn't make sense. if there is an exception thrown, take out the line and try and figure out why. you could take out the "args[i]" and just print i. you'd see it printing values 1,2,3 and 4. then you can try and figure out why it's that instead of 0,1,2,3


I tried earlier to get him to do that, Fred. But I guess he's a theoretical programmer
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic