- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个程序,该程序从文件“items.txt”获取输入,并根据“changes.txt”添加或删除行,然后输出到另一个文件。
这是“items.txt”文件:
101,Nail #1
102,Nail #2
103,Nail #3
104,Hammer Small
105,Hammer Large
这是更改文件:
A,106,Chainsaw 12"
D,102
d,104
a,107,Chainsaw 10"
这是我的方法,有问题(voidchanges()
是第 132 行):
void changes() {
try {
fileChange = new Scanner(new File("changes.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
fileChange.useDelimiter(",|\r|\n");
while (fileChange.hasNext()) {
String codeStr = fileChange.next();
if ((codeStr.charAt(0) == 'D') || (codeStr.charAt(0) == 'd')) {
delete(fileChange.nextInt());
System.out.println("delete");
} else if ((codeStr.charAt(0) == 'A') || (codeStr.charAt(0) == 'a')) {
add(fileChange.nextInt(), fileChange.next());
System.out.println("add");
} //else
System.out.println("done");
}
fileChange.close();
}
这是我得到的输出:
add
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
done
我在打印语句中添加了“添加”和“完成”只是为了帮助尽早诊断问题。即使它打印“add”和“done”,也不会发送任何内容到输出文件。我猜这是由于 StringIndexOutOfBoundsException 造成的
这是我在 Stack Overflow 上的第一个问题,因此请对任何格式问题或不当礼仪保持耐心。
最佳答案
原因是您使用 ",|\r|\n"
作为分隔符。这将返回换行符的 \r
和 \n
之间的空标记(假设您使用的是 Windows 样式的换行符)。例如:
Scanner sc = new Scanner(new StringReader("Hello\r\nWorld"));
sc.useDelimiter(",|\r|\n");
while (sc.hasNext()) {
String n = sc.next();
System.out.println(n.length() + " " + n);
}
输出:
5 Hello
0
5 World
因此,对于那些零长度标记,您无法读取 charAt(0)
,因为没有这样的字符。
将分隔符更改为 ",|\r\n"
之类的内容,即将 \r\n
视为单个分隔符。
关于Java StringIndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42590114/
我正在编写一个程序,该程序从文件“items.txt”获取输入,并根据“changes.txt”添加或删除行,然后输出到另一个文件。 这是“items.txt”文件: 101,Nail #1 102,
我创建了一个应该清理String的方法。它必须删除 [a,z] 范围内的每个字符。 但我收到此异常:StringIndexOutOfBoundsException。当我插入超出条件的数字时,就会弹出此
我正在制作的这个程序编译不正确,我不断收到错误:线程“main”中的异常 java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:-9 在 java.l
在输入整数后输入字符串“s”时出现此错误。 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String i
当我按下 Android 应用程序的“提交”按钮时,我遇到了应用程序崩溃的情况。该应用程序负责发送输入的数据,将其作为事件添加到用户的日历中。仅当日期字段留空时应用程序才会崩溃。我注意到存储日期分割部
我正在解决一个问题,如果相邻字符具有相同的值,我必须从字符串中删除字符。这是我的代码: import java.util.Scanner; public class SuperReducedStrin
我必须创建一个数字内存游戏,但如果用户输入错误的数字,我会收到 StringIndexOutOfBoundsException。我看了很多例子,但仍然不知道如何解决它。这发生在 actionPerfo
我编写了一个静态方法,旨在获取字符串并将其打乱。 当您输入给定的字符串,例如“四分七”时,您应该得到输出 “f rneosedvuc eroasn”,因为字符串被打乱到下面的网格中。 row 1:
我有以下代码 public class test{ public static void main(String[] args){ String one = "x";
当我尝试放置一个带有十六进制颜色的字符串变量来设置我的适配器类上的 edittext 的 setbackgroundcolor(Color.parseColor(String s)) 时,它说 Str
import java.io.* ; class Specimen { public static void main(String[] args) throws IOException
我在 Java 中收到以下错误消息 Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String i
我有这个代码: String input = JOptionPane.showInputDialog("Enter text"); StringBuilder forward = new String
我有以下代码 while (input.hasNextLine()) { String line = input.nextLine(); System.out.println(line
在 stackoverflow 和其他论坛上寻找解决方案一个小时后,我放弃了。 您知道我为什么会遇到异常吗? public Stack einlesen(){ Scanner sc =
为什么下面的 Java 代码片段在第三行代码中抛出 StringIndexOutOfBoundsException? String str = "1234567890"; String sub1 =
这个问题不太可能对任何 future 的访客有帮助;它只与一个较小的地理区域、一个特定的时间点或一个非常狭窄的情况相关,通常不适用于全世界的互联网受众。如需帮助使此问题更广泛适用,visit the
这应该隔离姓名和分数,然后计算分数的平均值。输出应如下所示: Student Name Test 1 Test 2 Test 3 Final Avera
这个错误是在子字符串方法上抛出的,我发现很多线程都在处理这个问题,但我遇到的问题似乎有所不同。我知道如果您的字符串短于子字符串(开始,结束)大小,它会抛出此错误,但在任何内容传递到方法调用之前都会抛出
我正在使用 java 编写一个密码学/密码分析程序作为我主人的家庭作业。无论如何,我使用一种方法来删除无用的空格并使我显示的字符串正确地显示在 JTextArea 中。此方法非常适合小文本,但当我使用
我是一名优秀的程序员,十分优秀!