gpt4 book ai didi

java - 线程中的异常 "main"java.lang.StringIndexOutOfBoundsException : String index out of range:

转载 作者:行者123 更新时间:2023-12-01 10:43:18 25 4
gpt4 key购买 nike

我的代码是:

import java.util.*;
public class Lab4Exercise {

public static String reverseString(String a){
int x = a.length();
String b = "";
while(x > 0){
int y = x - 1;
b = b + a.charAt(y);
x--;
}
return b;
}
public static String encrypt(String a){
String a1 = a.replaceAll("\\p{Punct}+", "");
String a2 = a1.replaceAll(" ", "");
String str = "";
String str2 = "";
if(a.length() % 2 == 0){
for(int q = 0; q <= a2.length(); q += 2){
str = str + a.charAt(q);
}
for(int w = 1; w < a2.length(); w += 2){
str2 = str2 + a.charAt(w);
}
return str.concat(str2);
}
else{
for(int z = 0; z < a2.length(); z += 2){
str2 = str2 + a.charAt(z);
}
for(int c = 1; c <= a2.length(); c += 2){
str2 = str2 + a.charAt(c);
}
return str.concat(str2);
}
}
public static int countTool(String a){
a = a.toLowerCase();
int g = a.length();
String str = "";
while(g >= 0){
if(a.charAt(g) == 'a'){
str = str + a.charAt(g);
}
g--;
}
int h = str.length();
return h;
}
public static double Equation(int n, int x){
double a = 0, t = 0;
if(n >= 0){
double w = 0.0;
for(int k = 0; k <= n; k++){
a = Math.pow(x, k)*Math.pow(Math.E, (n - k));
w = w + a;}
return Math.abs(w);
}
else {
double q = 0;
for(int b = 0; b >= n; b--){
t = Math.pow(x, b)*Math.pow(Math.E, (n - b));
q = q + t;}
return Math.abs(q);
}
}

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter case number: ");
int caseVal = scan.nextInt();
switch(caseVal){

case 1:
System.out.println("Enter a string to reverse: ");
String str = scan.next();
System.out.println("Reversed string is: " + reverseString(str));
break;

case 2:
System.out.println("Enter a string to encrypt");
String str2 = scan.next();
System.out.println("Encrypted text is: " + encrypt(str2));
break;

case 3:
System.out.println("Enter a string to count: ");
String str3 = scan.next();
System.out.println(countTool(str3));
break;

case 4:
System.out.println("Enter n value: ");
int n = scan.nextInt();
System.out.println("Enter x value: ");
int x = scan.nextInt();
System.out.println(Equation(n, x));
break;

case 5:
Random random = new Random();
int randomnum = 0;
for(int p = 0; p < 50; p++){
randomnum = 20 + (int)(Math.random()*119);
System.out.println(Math.sqrt(randomnum));
}
}
}

}



因此,当我尝试执行案例 2 和案例 3 时,它会给出此错误:(例如,我在这两种情况下都选择了“我爱树!”)

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)
at Lab4Exercise.encrypt(Lab4Exercise.java:33)
at Lab4Exercise.main(Lab4Exercise.java:84)

对于案例3:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(Unknown Source)<br/>
at Lab4Exercise.countTool(Lab4Exercise.java:43)<br/>
at Lab4Exercise.main(Lab4Exercise.java:90)<br/>

最佳答案

第 33 行,您正尝试访问索引处不存在的数组。

改变

for(int c = 1; c <= a2.length(); c += 2){

 for(int c = 1; c <a2.length(); c += 2){

玩弄字符串的大小时要小心。如果字符串的大小为 8,这意味着您可以从索引 0 到 7 访问它,是的,在 Debug模式下运行代码并继续检查值,这确实会对您有所帮助。

关于java - 线程中的异常 "main"java.lang.StringIndexOutOfBoundsException : String index out of range:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34332879/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com