Java - java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:11
Java - java.lang.StringIndexOutOfBoundsException: String index out of range: 11
我被这个问题困住了,找不到解决方案。在线程 "main" java.lang.StringIndexOutOfBoundsException 中获取异常错误:字符串索引超出范围:11.
谁能帮忙解决这个问题?
我的代码:
public static void main(String[] args) {
try {
Scanner sc = new Scanner(new File("Testing.txt"));
int i = 0;
while(sc.hasNext()){
String line = sc.nextLine();
char needle = line.charAt(i);
while(i < line.length()){
if(Character.isUpperCase(needle)) {
while(needle != ' '){
System.out.print(needle);
i++;
needle = line.charAt(i);
}
System.out.println(needle);
}
else{
i++;
needle = line.charAt(i);
}
}
}
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
根据我上面的评论,使用 String split
方法会更容易。当然 input
可以替换为文件中的文本,使用 expectedResult
并测试输出是否等于完全可选
public static void main(String[] args)
{
String input = "Kenny and I are eating in the Restaurant Yummy";
String expectedResult = "Kenny, I, Restaurant, Yummy";
String arr [] = input.split (" ");
StringBuilder out = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
if (Character.isUpperCase(arr[i].charAt(0))) {
out.append(arr[i]).append(", ");
}
}
if (out.length() > 1) {
out.setLength(out.length() -2);
}
System.out.println (out);
assert (expectedResult.equals(out.toString()));
}
输出
Kenny, I, Restaurant, Yummy
我不确定你想要达到什么目的。但是从你的错误来看,我可以说你正在尝试增加索引计数器,然后尝试访问你的字符串中的元素。
在你的 while
循环和 else
块中,你有这些语句:
i++;
needle = line.charAt(i);
将它们都更改为:
needle = line.charAt(i);
i++;
并且您的代码 运行 没有上述异常。
Update
我做了上面提到的更改,并做了一些更改。
- 在第一个 while 循环中包含
int i=0;
的声明。文本文件中多于一行需要这样做。
- 在第二个 while 循环中包含声明
char needle = line.charAt(i);
语句,因为您需要为每一行初始化 needle。
try (Scanner sc = new Scanner(new File("Testing.txt"))) {
while (sc.hasNext()) {
String line = sc.nextLine();
int i = 0;
while (i < line.length()) {
char needle = line.charAt(i);
if (Character.isUpperCase(needle)) {
while (needle != ' ' && i < line.length()) {
needle = line.charAt(i);
if(needle != ' '){
System.out.print(needle);
}
i++;
}
if(i < line.length()) {
System.out.print(", ");
}
} else {
needle = line.charAt(i);
i++;
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
它打印以下输出:
Kenny, I, Restaurant, Yummy
希望对您有所帮助!
我的假设是你的错误会在这里;
i++;
needle = line.charAt(i);
和
while(needle != ' '){
System.out.print(needle);
i++;
needle = line.charAt(i);
}
因为您增加索引而不检查索引是否存在。
我被这个问题困住了,找不到解决方案。在线程 "main" java.lang.StringIndexOutOfBoundsException 中获取异常错误:字符串索引超出范围:11.
谁能帮忙解决这个问题? 我的代码:
public static void main(String[] args) {
try {
Scanner sc = new Scanner(new File("Testing.txt"));
int i = 0;
while(sc.hasNext()){
String line = sc.nextLine();
char needle = line.charAt(i);
while(i < line.length()){
if(Character.isUpperCase(needle)) {
while(needle != ' '){
System.out.print(needle);
i++;
needle = line.charAt(i);
}
System.out.println(needle);
}
else{
i++;
needle = line.charAt(i);
}
}
}
}
catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
根据我上面的评论,使用 String split
方法会更容易。当然 input
可以替换为文件中的文本,使用 expectedResult
并测试输出是否等于完全可选
public static void main(String[] args)
{
String input = "Kenny and I are eating in the Restaurant Yummy";
String expectedResult = "Kenny, I, Restaurant, Yummy";
String arr [] = input.split (" ");
StringBuilder out = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
if (Character.isUpperCase(arr[i].charAt(0))) {
out.append(arr[i]).append(", ");
}
}
if (out.length() > 1) {
out.setLength(out.length() -2);
}
System.out.println (out);
assert (expectedResult.equals(out.toString()));
}
输出
Kenny, I, Restaurant, Yummy
我不确定你想要达到什么目的。但是从你的错误来看,我可以说你正在尝试增加索引计数器,然后尝试访问你的字符串中的元素。
在你的 while
循环和 else
块中,你有这些语句:
i++;
needle = line.charAt(i);
将它们都更改为:
needle = line.charAt(i);
i++;
并且您的代码 运行 没有上述异常。
Update
我做了上面提到的更改,并做了一些更改。
- 在第一个 while 循环中包含
int i=0;
的声明。文本文件中多于一行需要这样做。 - 在第二个 while 循环中包含声明
char needle = line.charAt(i);
语句,因为您需要为每一行初始化 needle。
try (Scanner sc = new Scanner(new File("Testing.txt"))) {
while (sc.hasNext()) {
String line = sc.nextLine();
int i = 0;
while (i < line.length()) {
char needle = line.charAt(i);
if (Character.isUpperCase(needle)) {
while (needle != ' ' && i < line.length()) {
needle = line.charAt(i);
if(needle != ' '){
System.out.print(needle);
}
i++;
}
if(i < line.length()) {
System.out.print(", ");
}
} else {
needle = line.charAt(i);
i++;
}
}
System.out.println();
}
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
它打印以下输出:
Kenny, I, Restaurant, Yummy
希望对您有所帮助!
我的假设是你的错误会在这里;
i++;
needle = line.charAt(i);
和
while(needle != ' '){
System.out.print(needle);
i++;
needle = line.charAt(i);
}
因为您增加索引而不检查索引是否存在。