我一直在创建一个 Java 程序来编码从用户收到的消息。只是为了了解它是如何做到这一点的一些背景知识:它应该获取输入,将其拆分为字符,然后从 Random.org(真正的随机数生成器)获取一组等于消息长度的随机数,然后移位输入的字符通过其相应的移位或随机数,然后输出编码的消息和移位。到目前为止,我已经获得了输入,将其转换为字符串数组,检查了配额(Random.org 有配额)并获得了随机数。尝试输出转换后的班次时出现此错误(从网站上获得的字符串到整数),我认为这是因为最后一个数字上有一个 CRLF(我尝试使用正则表达式来解决这个问题,但它没有工作)。这是我的代码:
公共类 Encryption_System { 静态字符串 originalMessege;
public static void main(String[] args) throws Exception {
System.out.println("Welcome to the encryption system!");
System.out.println("Type your messege below:");
System.out.println("\nHere is your original messege: " + scan() + "\n");
Encrypt code = new Encrypt();
code.Messege(originalMessege);
code.encryptMessege();
}
private static String scan() {
Scanner scan = new Scanner(System.in);
originalMessege = scan.nextLine();
return originalMessege;
}
}
然后是第二节课,我的问题就出在这里。我的错误来自最后一种方法(我尝试的正则表达式修复被注释掉了):
public class Encrypt {
private String messege;
private String[] characters;
private URL quotaURL;
private URLConnection conect;
private InputStream quotaInput;
private BufferedReader quotaReader;
private int quota;
private boolean go;
private URL shiftsURL;
private URLConnection conectShifts;
private InputStream shiftsInput;
private BufferedReader shiftsReader;
private int count;
private char[] shifts;
private int[] shiftsInt;
private String shiftsString;
private String[] shiftsStrings;
public void Messege(String x) {
messege = x;
}
private String[] getCharacters() {
characters = messege.split("(?!^)");
return characters;
}
private boolean checkQuota() throws Exception {
quotaURL = new URL("http://www.random.org/quota/?format=plain");
conect = quotaURL.openConnection();
quotaInput = conect.getInputStream();
quotaReader = new BufferedReader(new InputStreamReader(quotaInput));
int quota = Integer.parseInt(quotaReader.readLine());
if (quota >= getCharacters().length)
go = true;
else
go = false;
return go;
}
private char[] Shifts(String[] x1) throws Exception {
String[] messegeArray = x1;
count = 0;
for (int k = 0; k < x1.length; k++) {
if (x1[k].equals(" ")) {
continue;
} else {
count++;
}
}
shifts = new char[count * 3];
if (checkQuota() == true) {
shiftsURL = new URL("http://www.random.org/integers/?num=" + count
+ "&min=1&max=27&col=" + count
+ "&base=10&format=plain&rnd=new");
conectShifts = shiftsURL.openConnection();
shiftsInput = conectShifts.getInputStream();
shiftsReader = new BufferedReader(
new InputStreamReader(shiftsInput));
shiftsReader.read(shifts, 0, count * 3);
}
return shifts;
}
public void encryptMessege() throws Exception {
char[] currentShifts = Shifts(getCharacters());
shiftsString = new String(currentShifts);
// shiftsString.replace("[\t\r\n]", "");
shiftsStrings = shiftsString.split("[( )]");
shiftsInt = new int[shiftsStrings.length];
System.out.println("These are your shifts");
for (int v = 0; v < shiftsInt.length; v++) {
shiftsInt[v] = Integer.parseInt(shiftsStrings[v]);
System.out.println(shiftsInt[v] + " ");
}
}
}
这是我的输出:
Welcome to the encryption system!
Type your messege below:
Hello
Here is your original messege: Hello
These are your shifts
3
19
12
3
Exception in thread "main" java.lang.NumberFormatException: For input string: "12
Process completed.
我只是一个Java初学者(高中一年级comp sci),所以感谢您的帮助!