java输入读取器无法识别文件的特定值
这是我的代码,用于读取一行并理解输入,即制表符字符之前的所有内容,以及标签,即制表符之后的所有内容:java输入读取器无法识别文件的特定值,java,Java,这是我的代码,用于读取一行并理解输入,即制表符字符之前的所有内容,以及标签,即制表符之后的所有内容: // READ INPUT BufferedReader reader = new BufferedReader(new FileReader("../PA-A-train.dat")); while (reader.readLine() != null) { String[] label_detector = reader.readLine(
// READ INPUT
BufferedReader reader = new BufferedReader(new FileReader("../PA-A-train.dat"));
while (reader.readLine() != null)
{
String[] label_detector = reader.readLine().split("\t");
String trueLabel = label_detector[label_detector.length - 1];
String inputs = label_detector[label_detector.length - 2];
System.out.println("these are inputs: " + inputs);
System.out.println("this is the corresponding label: " + trueLabel);
}
reader.close();
这是我的档案:
0 0 0 0 0 A 0 0
0 0 0 0 1 B 0 0
0 0 0 1 0 C 0 0
0 0 0 1 1 0 0
0 0 1 0 0 0 0
0 0 1 0 1 0 0
0 0 1 1 0 0 0
0 0 1 1 1 1 0
0 1 0 0 0 0 0
0 1 0 0 1 0 1
0 1 0 1 0 0 0
0 1 0 1 1 1 1
0 1 1 0 0 0 0
0 1 1 0 1 1 1
0 1 1 1 0 1 0
0 1 1 1 1 1 1
1 0 0 0 0 0 0
1 0 0 0 1 0 0
1 0 0 1 0 0 0
1 0 0 1 1 1 0
1 0 1 0 0 0 0
1 0 1 0 1 1 0
1 0 1 1 0 1 0
1 0 1 1 1 1 0
1 1 0 0 0 0 0
1 1 0 0 1 1 1
1 1 0 1 0 1 0
1 1 0 1 1 1 1
1 1 1 0 0 1 0
1 1 1 0 1 1 1
1 1 1 1 0 1 0
1 1 1 1 1 1 1
但我的输出如下所示:
these are inputs: 0 0 0 0 1 B
this is the corresponding label: 0 0
these are inputs: 0 0 0 1 1
this is the corresponding label: 0 0
these are inputs: 0 0 1 0 1
this is the corresponding label: 0 0
these are inputs: 0 0 1 1 1
this is the corresponding label: 1 0
these are inputs: 0 1 0 0 1
this is the corresponding label: 0 1
these are inputs: 0 1 0 1 1
this is the corresponding label: 1 1
these are inputs: 0 1 1 0 1
this is the corresponding label: 1 1
these are inputs: 0 1 1 1 1
this is the corresponding label: 1 1
these are inputs: 1 0 0 0 1
this is the corresponding label: 0 0
these are inputs: 1 0 0 1 1
this is the corresponding label: 1 0
these are inputs: 1 0 1 0 1
this is the corresponding label: 1 0
these are inputs: 1 0 1 1 1
this is the corresponding label: 1 0
these are inputs: 1 1 0 0 1
this is the corresponding label: 1 1
these are inputs: 1 1 0 1 1
this is the corresponding label: 1 1
these are inputs: 1 1 1 0 1
this is the corresponding label: 1 1
these are inputs: 1 1 1 1 1
this is the corresponding label: 1 1
如您所见,第一行完全被忽略,它也忽略了第三行。为什么呢
**建议的解决方案如下所示:*
String line;
while ((line = reader.readLine()) != null)
{
String[] label_detector = line.split("\t");
String trueLabel = label_detector[label_detector.length - 1];
String inputs = label_detector[label_detector.length - 2];
System.out.println("these are inputs: " + inputs);
System.out.println("this is the corresponding label: " + trueLabel);
}
reader.close();
每次迭代调用
readLine()
两次。偶尔
while (reader.readLine() != null)
一旦
String[] label_detector = reader.readLine().split("\t");
将代码更改为
String line;
while ((line = reader.readLine()) != null) {
String[] label_detector = line.split("\t");
不仅是这两行,还有大约一半的行丢失了。问题出在
reader.readLine()
上。你读了两行,但只处理了一行
你可以这样修理它
// READ INPUT
BufferedReader reader = new BufferedReader(new FileReader("../PA-A-train.dat"));
String line;//new variable
while ((line=reader.readLine()) != null) //read the line and compare
{
String[] label_detector = line.split("\t");//split
String trueLabel = label_detector[label_detector.length - 1];
String inputs = label_detector[label_detector.length - 2];
System.out.println("these are inputs: " + inputs);
System.out.println("this is the corresponding label: " + trueLabel);
}
reader.close();