Forums Register Login

string reverse

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send

Do you need that semicolon???
But that's not all the code is it?
[ April 02, 2003: Message edited by: Barry Gaunt ]
+Pie Number of slices to send: Send

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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
Thanks for the help everyone...
yuan
This is my favorite tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1223 times.
Similar Threads
Propogating Exceptions - Exercise5.3 in K&B
Java Util packages
help me out!!! i m new 2 java
Swing Program with Buttons
string reversal problem
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 02:56:56.