aspose file tools*
The moose likes Beginning Java and the fly likes help with this program code please!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with this program code please!!" Watch "help with this program code please!!" New topic
Author

help with this program code please!!

Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
Hi, Ranchers,this is my home assignment.It is suppose to reverse a hard coded string, but it gives a run time error.I will post the Error message here as well as the code it self.
looking forward for your kind help.

the error message is :
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String ind
ex out of range: 7
at java.lang.String.charAt(String.java:687)
at Reverse.function(Reverse.java:13)
at Reverse.main(Reverse.java:19)
Press any key to continue . . .
[ January 14, 2008: Message edited by: Ammar Salem ]

****************************<br />For Some Dreams.....<br />One Lifetime Is Not Enough.<br />****************************
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

things in java are usually 0-base indexed. so, if an array has 5 elements, they are numbered from 0 through 4...

That's a big hint.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ammar Salem
Ranch Hand

Joined: Dec 29, 2007
Posts: 60
Originally posted by Fred Rosenberger:
things in java are usually 0-base indexed. so, if an array has 5 elements, they are numbered from 0 through 4...

That's a big hint.



Still I dont get it.Sorry !!
Abhinav Srivastava
Ranch Hand

Joined: Nov 19, 2002
Posts: 349

try to find at what index it is throwing the exception and then why is it reaching that index.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

look at your error:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 7


now, look at the api for charAT().

What value is your strlen() returning? what are the allowed indexes for charAt()?
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Dear,

It is as simple as this:

REVERSE ---> length ---> 7
charAt(0) --> R
charAt(1) --> E
charAt(2) --> v
charAt(3) --> E
charAt(4) --> R
charAt(5) --> S
charAt(6) --> E

now look what you are trying to access at first iteration ?


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Rohan Kalbhor
Ranch Hand

Joined: Aug 18, 2006
Posts: 78
dont use String
use StringBuffer.

Please see its implementation in java docs.

Its very easy then


..............................<br />Exceptions are a part of possibility<br />Errors are a part of truth<br />................................
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Use StringBuilder if you are not in a threaded environment; it is faster than StringBuffer.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

I would say the way you are doing it is fine, at the level you are currently at. When you get more comfortable with programming, you can research why a StringBuffer or StringBuilder might be a better choice, but for now, I'd stick with what you have.

you are SO CLOSE. making one small change should get this to work.

Sunny's post practically gives the answer away.
Kevin Cahill
Greenhorn

Joined: Jan 08, 2008
Posts: 9
The method I used to figure this out was to print out each value as follows:

System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println(s.charAt(2));
System.out.println(s.charAt(3));
System.out.println(s.charAt(4));
System.out.println(s.charAt(5));
System.out.println(s.charAt(6));

After I did that, it was then obvious for me what the problem was.


Persistence and determination alone are omnipotent.<br />---Calvin Coolidge
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Well done
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: help with this program code please!!