• 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

increment operator

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why does this code
public class strprint {
public static void main (String[] args) {
StringBuffer str1 = new StringBuffer(50) ;
for(int i=0; i++<args.length {>
str1 = str1.append(args[i]);
str1 = str1.append(" "); }
System.out.println(str1);
}
}
give me an ArrayOutOfBoundsException ?
strangely, when I substitute with this line
for(int i=0; i<args.length;i++ )>
it works ! (ideally I think this should not happen ?)
I'm confused !!
 
Ranch Hand
Posts: 173
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is with line
str1 = str1.append(args[i]); when the argument list is out of bound, then obviously it would give the ArrayOutofBoundException.
I have given the arguments
java hello pal
and after printing these two it gives the Exception.So with minor changes to your code:

public class Strprint {
public static void main (String[] args) {
StringBuffer str1 = new StringBuffer(50) ;
for(int i=0; ;i++)
{str1 = str1.append(args[i]);
System.out.println(args[i]);
str1 = str1.append(" ");
}
//System.out.println(str1);
}
}
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is more readeable by using the [ c o d e ] and [ / c o d e ] (remove spaces) UBB tags respectively before and after your code samples.
You must also put spaces around the < character!
Like this:

PS: To learn more about UBB, click HERE.
Cheers,
Beno�t
[This message has been edited by Beno�t d'Oncieu (edited November 01, 2000).]
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It gives ArrayIndexOutOfBoundsException for the last element.
This is because first condition is checked and then immdiately it increaments i.
so for last element value of i is actualy args.length.
here is some alternate working code:-

public class strprint {
public static void main (String[] args) {
StringBuffer str1 = new StringBuffer(50) ;
for(int i=0; i < args.length; ) {
str1 = str1.append(args[i++]);
str1 = str1.append(" "); }
System.out.println(str1);
}
}
value of i should be incremented after use in args[i]

[This message has been edited by rajesh dalvi (edited November 01, 2000).]
 
This guy is skipping without a rope. At least, that's what this tiny ad said:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic