I'm making a program that will take an input string and decode it using the Rot13 encryption method. This takes the alphabet, and rotates it by 13.
I'm having a hard time getting the index of a letter in the list, and every time I run it it gives me -1 as if the item is not in the list. I looked in the java documentation, and indexOf() asks for an object. I tried explicitly typing my input as an object but that didn't work either.
This is the code I have so far:
package rot13;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
/**
*
* @author andrewjohnson
*/
public class CipherKey {
List<String> alpha = Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", " ");
List<String> alphaRev = Arrays.asList("Z", "Y", "X", "W", "V", "U", "T", "S", "R", "Q", "P", "O", "N", "M", "L", "K", "J", "I", "H", "G", "F", "E", "D", "C", "B", "A", " ");
public String codeDecode(String s) {
System.out.println(s);
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
//System.out.println(ch);
int x = alpha.indexOf(ch);
//System.out.println(x);
String y = alphaRev.get(x);
System.out.print(y);
}
return null;
}
public static String readInput() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter message to be encoded or decoded");
String s = br.readLine().toUpperCase();
//System.out.println(s);
return s;
}
}
And my main():
/**
*
* @author andrewjohnson
*/
public class Rot13 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
CipherKey x = new CipherKey();
x.codeDecode(x.readInput());
}
}
I'm not sure why it is not working, but I've narrowed it down to the line:
int x = alpha.indexOf(ch);
Not being able to find ch in alpha. I'm new to Java and I've tried everything that I can think of. Thanks for your suggestions!
The problem is here:
char ch = s.charAt(i);
int x = alpha.indexOf(ch); // <-------- HERE
You are searching for a char
in a String
array. Which of course doesn't exist. Hense the -1
Change it to int x = alpha.indexOf("" + ch);
or int x = alpha.indexOf(Character.toString(ch));
or int x = alpha.indexOf(String.valueOf(ch));
Any of these will do.
ch
is of type char
and your list contains String
. List may accept Object
for indexOf
, but the type still counts.
Change int x = alpha.indexOf(ch);
to int x = alpha.indexOf(String.valueOf(ch));
to fix it.
Example:
System.out.println(alpha.indexOf('D'));
System.out.println(alpha.indexOf(String.valueOf('D')));
will print
-1
3
That is not the rot13 algorithm - you just appear to be reversing the alphabet. rot13 maps the range A - M
to N - Z
, and vice versa. Two invocations of rot13 give you back the original text.
ASCII letters follow a numerical sequence. Instead of performing a linear search through a list to find the matching index, it's far faster to just calculate the difference between the current letter and A
, and then use that difference to offset into a second array of characters (or a string).
ie
static String map = "NOPQRSTUVWXYZABCDEFGHIJKLM"; // for rot13
function rot13(String input) {
StringBuffer output;
for (char ch : input) {
int index = ch - 'A';
if (ch >= 0 && ch < 26) {
output.append(map.charAt(index));
} else {
output.append(ch);
}
}
return output.toString();
}
NB: untested, may not compile, E&OE etc
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.