Ayuda en un deber cifrado cesar
Publicado: 27 Ago 2012, 23:56
Mire el profe me mando a hacer un cifrado cesar ingresar por teclado y eso lo codifica lo brinco que se debe dar no especifico al principio no me salia le meti char y ahi me valio, me sale el programa pero siempre al final me bota la respuesta junto con un error este:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:695)
at Cifrado_Cesar.main(Cifrado_Cesar.java:45)
no se lo que significa o porque me sale eso abajo pondre la programacion que yo hice si no es mucha molestia le pido que me ayuden gracias
import java.util.Scanner;
class Cifrado_Cesar {
public static void main (String[] args) {
char str [] = new char [27];
str[0]= 'a';
str[1]= 'b';
str[2]= 'c';
str[3]= 'd';
str[4]= 'e';
str[5]= 'f';
str[6]= 'g';
str[7]= 'h';
str[8]= 'i';
str[9]= 'j';
str[10]= 'k';
str[11]= 'l';
str[12]= 'm';
str[13]= 'n';
str[14]= 'ñ';
str[15]= 'o';
str[16]= 'p';
str[17]= 'q';
str[18]= 'r';
str[19]= 's';
str[20]= 't';
str[21]= 'u';
str[22]= 'v';
str[23]= 'w';
str[24]= 'x';
str[25]= 'y';
str[26]= 'z';
Scanner lectura = new Scanner (System.in);
System.out.println ("Ingrese el texto:");
String texto = lectura.next();
int longitud = texto.length();
System.out.println ("Ingrese cuanto espacio brincar");
int n= lectura.nextInt();// el numero de brinco
int abc= 0;
int i=-1;
int opcion= 4;
int l=0;
while(l<1){
while(i<longitud){
i++;
char cod = texto.charAt(i);
int k=0;
while(k<1){
if(cod == str[abc]){
abc=abc+n;
System.out.print ("\t" +str[abc]+" ");
k=k+1;
}else{
abc++;
}
}
abc = 0;
}
l=l+1;
}
}
}
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
at java.lang.String.charAt(String.java:695)
at Cifrado_Cesar.main(Cifrado_Cesar.java:45)
no se lo que significa o porque me sale eso abajo pondre la programacion que yo hice si no es mucha molestia le pido que me ayuden gracias
import java.util.Scanner;
class Cifrado_Cesar {
public static void main (String[] args) {
char str [] = new char [27];
str[0]= 'a';
str[1]= 'b';
str[2]= 'c';
str[3]= 'd';
str[4]= 'e';
str[5]= 'f';
str[6]= 'g';
str[7]= 'h';
str[8]= 'i';
str[9]= 'j';
str[10]= 'k';
str[11]= 'l';
str[12]= 'm';
str[13]= 'n';
str[14]= 'ñ';
str[15]= 'o';
str[16]= 'p';
str[17]= 'q';
str[18]= 'r';
str[19]= 's';
str[20]= 't';
str[21]= 'u';
str[22]= 'v';
str[23]= 'w';
str[24]= 'x';
str[25]= 'y';
str[26]= 'z';
Scanner lectura = new Scanner (System.in);
System.out.println ("Ingrese el texto:");
String texto = lectura.next();
int longitud = texto.length();
System.out.println ("Ingrese cuanto espacio brincar");
int n= lectura.nextInt();// el numero de brinco
int abc= 0;
int i=-1;
int opcion= 4;
int l=0;
while(l<1){
while(i<longitud){
i++;
char cod = texto.charAt(i);
int k=0;
while(k<1){
if(cod == str[abc]){
abc=abc+n;
System.out.print ("\t" +str[abc]+" ");
k=k+1;
}else{
abc++;
}
}
abc = 0;
}
l=l+1;
}
}
}