[英]Why does my program give an error when reading from file?
I seem to be encountering an error when reading from a text file. 从文本文件读取时,似乎遇到了错误。 This program should read a line, check the first Character, and run the related code in the if statement.
该程序应读取一行,检查第一个字符,然后在if语句中运行相关代码。 The program runs fine for the first line, and outputs the contents, however It cannot process the next line.
程序在第一行运行良好,并输出内容,但是无法处理下一行。 Here is the code im using:
这是即时通讯使用的代码:
public void importStart(){
try {
FileReader fr = new FileReader("src/data.txt");
BufferedReader reader = new BufferedReader(fr);
String line = reader.readLine();
Scanner scan = null;
while(line != null){
scan = new Scanner(line);
String string1 = scan.next();
inputType = string1.charAt(0);
if(inputType == 'S'){
foxCount = scan.nextInt();
rabbitCount = scan.nextInt();
dragonCount = scan.nextInt();
System.out.println(inputType + " "+ foxCount + " "+ rabbitCount + " "+ dragonCount);
}
else if(inputType == 'X'){
System.out.println("Test 1");
animalType = string1.substring(2, 3);
System.out.println("Test 2");
if(animalType == "F"){
step = scan.nextInt();
}
else if(animalType == "R"){
step = scan.nextInt();
}
else if(animalType == "D"){
step = scan.nextInt();
}
System.out.println(inputType + " "+ animalType + " " + step);
}
line = reader.readLine();
}
reader.close();
}
and I receive this error 我收到这个错误
Note, the First line is output that should be there, this is how I know it has operated on the first line correctly. 注意,第一行是应该在此处输出的,这就是我知道它在第一行上正确运行的方式。 The "Test 1" also appears correctly, which leads me to believe the problem is with my string1.substring implementation.
“测试1”也正确显示,这使我相信问题出在我的string1.substring实现上。 Is this the problem?
这是问题吗?
S 74 199 15
Exception in thread "AWT-EventQueue-0" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
Test 1
at java.lang.String.substring(Unknown Source)
at DataDisplayGui.importStart(DataDisplayGui.java:107)
at DataDisplayGui.actionPerformed(DataDisplayGui.java:178)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
String string1 = scan.next();
scan.next() gives you the next token in the line variable, which might not be the whole line. scan.next()在line变量中给您下一个标记,它可能不是整行。 Try replacing the offending substring line with:
尝试将有问题的子字符串行替换为:
animalType = line.substring(2, 3);
Depends what's in your src/data.txt file. 取决于src / data.txt文件中的内容。 If the line you read was
如果您读的那行是
X
you'd get this error because the statement 您会收到此错误,因为该语句
string1.substring(2,3)
is trying to return a substring, starting at character 2 正在尝试返回从字符2开始的子字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.