【发布时间】:2014-10-01 22:02:38
【问题描述】:
我正在制作一个程序,它将接受一个输入字符串并使用 Rot13 加密方法对其进行解码。这需要字母表,并将其旋转 13。
我很难获得列表中字母的索引,每次运行它都会给我-1,就好像该项目不在列表中一样。我查看了 java 文档, indexOf() 要求一个对象。我尝试将我的输入显式输入为对象,但这也不起作用。
这是我到目前为止的代码:
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;
}
}
还有我的 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());
}
}
我不确定为什么它不起作用,但我已将其范围缩小到以下范围:
int x = alpha.indexOf(ch);
无法在 alpha 中找到 ch。我是 Java 新手,我已经尝试了所有我能想到的东西。感谢您的建议!
【问题讨论】:
标签: java list encryption indexof