我正在学习 Java,我们有一个项目来制作一个将文本转换为 ASCII 并返回的程序。
目前我的主要方法是
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char decisionOne;
System.out.println("Are we making a new message or decrypting an old one?");
decisionOne = s.nextLine().charAt(0);
switch(decisionOne) {
case 'd':
decrypt();
System.exit(0);
break;
case 'D':
decrypt();
System.exit(0);
break;
case 'E':
encrypt();
System.exit(0);
break;
case 'e':
encrypt();
System.exit(0);
break;
}
while (!(decisionOne == 'd') && !(decisionOne == 'D') && !(decisionOne == 'e') && !(decisionOne == 'E')) {
System.out.println("Hey! Choose one of them.");
decisionOne = s.nextLine().charAt(0);
switch(decisionOne) {
case 'd':
decrypt();
System.exit(0);
break;
case 'D':
decrypt();
System.exit(0);
break;
case 'E':
encrypt();
System.exit(0);
break;
case 'e':
encrypt();
System.exit(0);
break;
}
}
}
我加密成 ASCII 的方法是
Scanner s = new Scanner(System.in);
System.out.println("What is your message?");
String message = s.nextLine();
char m;
int length = message.length();
int tracker = 0;
int ascii;
while (length >= 0 ) {
m = message.charAt(tracker);
length--;
ascii = (int)m;
System.out.print(ascii + " ");
tracker ++;
}
}
我环顾了其他问题,但似乎没有一个能回答这里发生的事情。当我运行时,我得到了正确的输出,所以如果我输入
<块引用>11
我会得到
<块引用>49 49 线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:2
我该怎么做才能解决这个问题?
答案 0 :(得分:0)
您想要 while (length > 0)
或 while (tracker < length)
或者你可以只做 for (Character m: message.toCharArray())
并放弃长度、跟踪器和 charAt 方法
答案 1 :(得分:0)
我把它放在你的加密方法中并且..
m = message.charAt(tracker);
System.out.print(length + " ");
length--;
ascii = (int)m;
System.out.print(ascii + " ");
tracker ++;
System.out.print(length + " "+tracker+" ");
我得到的输出是这个...
2 49 1 1 1 49 0 2
您在最后看到您的 tracker=2 超出了您的消息字符串的界限,这就是您收到异常的原因,您可以处理异常或改进您的代码... 个人经验——如果您是初学者,请使用 for 循环,它会让您更清楚代码的工作原理
答案 2 :(得分:0)
将加密方法中的代码从 while(length >= 0)
更改为 while(length - 1 >= 0)
。使用后也关闭scanner对象,因为它会导致内存泄漏。
length() 方法返回字符串的长度,(例如)String message = "Hello"; length = message.length();
长度将等于 5。但是在 while 循环中使用它时,您必须使用 message.length() - 1 因为在 Java 中索引从 0 开始,而不是从 1 开始。
我重构了你的 encypt 方法和 main 方法,而且我觉得没有必要使用 switch 语句。用 if else 语句替换它们以减少冗余代码。
如果你觉得这个解决方案有用,请点赞这个解决方案谢谢。
public static void encrypt() {
Scanner s = new Scanner(System.in);
System.out.println("What is your message?");
String message = s.nextLine();
char m;
int length = message.length();
int tracker = 0;
int ascii;
while (length - 1 >= 0 ) {
m = message.charAt(tracker);
length--;
ascii = (int)m;
System.out.print(ascii + " ");
tracker++;
}
s.close();
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
char decisionOne;
System.out.println("Are we making a new message [e (or) E] or decrypting an old one [d (or) D] ?");
decisionOne = s.nextLine().charAt(0);
if (decisionOne == 'd' || decisionOne == 'D') {
decrypt();
System.exit(0);
}
else if (decisionOne == 'e' || decisionOne == 'E') {
encrypt();
System.exit(0);
}
while (!(decisionOne == 'd') && !(decisionOne == 'D') && !(decisionOne == 'e') && !(decisionOne == 'E')) {
System.out.println("Hey! Choose one of them.");
decisionOne = s.nextLine().charAt(0);
if (decisionOne == 'd' || decisionOne == 'D') {
decrypt();
System.exit(0);
}
else if (decisionOne == 'e' || decisionOne == 'E') {
encrypt();
System.exit(0);
}
}
s.close();
}