here is the method:
public static String getEncrypted(String x){
char y;
String output = " ";
int count = 0, position = 0, place= 0;
int length = x.length();
while(count < length){
place = length - position;
y = x.charAt(place);
position++;
count++;
output = output + y;
}
return output;
}
I am able to compile the code which basically takes a string splits it up into the words that make it then feeds it to this method.
When I use the program and enter something to be used by this method I get the error message
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(Unknown Source)
at encrypt.getEncrypted(encrypt.java:37)
at encrypt.main(encrypt.java:23)
My question is what I am doing wrong and how do I fix it?
Change place = length - position;
to place = length - position - 1
; and try again
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments