Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

help with this program code please!!

 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ammar Salem
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 354
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try to find at what index it is throwing the exception and then why is it reaching that index.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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()?
 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Rohan Kalbhor
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont use String
use StringBuffer.

Please see its implementation in java docs.

Its very easy then
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use StringBuilder if you are not in a threaded environment; it is faster than StringBuffer.
 
fred rosenberger
lowercase baba
Bartender
Pie
Posts: 12098
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic