permaculture playing cards
The moose likes Beginning Java and the fly likes string reverse Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "string reverse" Watch "string reverse" New topic

string reverse

Yuan Tseng

Joined: Mar 11, 2003
Posts: 21
hi, everyone,
i was trying to do 'string reverse'. i have no compile error but when execute i got this error messages:
at Backwards.main(
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.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

Do you need that semicolon???
But that's not all the code is it?
[ April 02, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Elouise Kivineva
Ranch Hand

Joined: Feb 07, 2002
Posts: 154

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
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
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

Joined: Mar 11, 2003
Posts: 21
Thanks for the help everyone...
I agree. Here's the link:
subject: string reverse
It's not a secret anymore!