aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why do i get this run time eroor... plz help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Why do i get this run time eroor... plz help" Watch "Why do i get this run time eroor... plz help" New topic
Author

Why do i get this run time eroor... plz help

Anish Nagraj
Greenhorn

Joined: Feb 27, 2006
Posts: 20
My program is like this...

public class Propogate {

String reverse(String val) throws Exception{

String reverseStr ="";
System.out.println(val);
if(val.length()== 0){
throw new Exception();
}
else{

for(int i = val.length(); i>0; --i){
reverseStr = reverseStr + val.charAt(i);
}
val = reverseStr;
}
return val;

}
public static void main(String[] args) {

Propogate eh = new Propogate();
try{
System.out.println(eh.reverse("java"));
}catch(Exception e){
System.out.println(e);

}


}
}

i get a ru time error telling

java.lang.StringIndexOutOfBoundsException: String index out of range: 4
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
The first index in a String is position 0. So the last position is not the length of the String, but one less. You are trying to access the position in the String indicated by its length.
srilatha kareddy
Ranch Hand

Joined: Jan 12, 2006
Posts: 32
for(int i = val.length(); i>0; --i){
reverseStr = reverseStr + val.charAt(i);
}

val.length()=4;

val.charAt(4); ...Its not there so run-time exception occur

...this will work.........
for(int i = val.length()-1; i>=0; --i){
reverseStr = reverseStr + val.charAt(i);
}
Anish Nagraj
Greenhorn

Joined: Feb 27, 2006
Posts: 20
1. i want to know exactly when the pre-increment of i works out and the value of i becomes 2 in the program i specified.

2. i also want what difference does a pre-decrement and post-decrement in this program makes... hope it makes no difference... let me know the exact description...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why do i get this run time eroor... plz help
 
Similar Threads
Exception compiler error
K&B - Exercise - try/catch problem
sorting a array
missing return statement error message when compiling class
Propogating Exceptions - Exercise5.3 in K&B