This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

string reverse

 
Yuan Tseng
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, everyone,
i was trying to do 'string reverse'. i have no compile error but when execute i got this error messages:
java.lang.ArrayIndexOutOfBoundsException
at Backwards.main(Backwards.java:10)
Exception in thread "main"

and this is the code:
for (i=0; i < str.length; i++);
System.out.print(str[i] + " ");
System.out.println ("\n");
please help me out.
thanks,
yuan
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Do you need that semicolon???
But that's not all the code is it?
[ April 02, 2003: Message edited by: Barry Gaunt ]
 
Elouise Kivineva
Ranch Hand
Posts: 154
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This will reel through a string named str from front to back. If you want to step through from back to front try this:

Remember too that it's str.length if str is an array, but str.length() is str is a String.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One little tidbit to add:Your problem, as stated above, is the semicolon. This closes the for statement, and you're simply looping str.length() times in an empty loop.
At the end of the loop, i has the value str.length(), which is whygives you an ArrayIndexOutOfBoundsException (remember, arrays indexes go from 0 to array.length-1).
- Peter
 
Yuan Tseng
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help everyone...
yuan
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic