我一直在学习并堆叠一个问题。我尝试从文本文件中搜索特定名称和员工编号。我试图在网上研究,但我没有找到特别多的结果。
我该如何解决这个问题:
。\ txtFileReader.java:15:错误:找不到符号((line = filescan.readLine())!= null)^符号:方法readLine()位置: Scanner 1错误类型的可变文件扫描
这是我的代码:
File oldFile = new File("C:\\Tesseract-OCR\\" + name + ".txt");
try (BufferedReader reader = new BufferedReader(new FileReader(oldFile)))
{
StringBuilder contents = new StringBuilder();
String text;
while ((text = reader.readLine()) != null)
{
String lineSeparator = System.getProperty("line.separator");
contents.append(text).append(lineSeparator);
System.out.println(text);
if (text.contains("*Department: @INFO. TECH"))
{
int index = text.indexOf("TECH");
text = text.substring(index, text.length());
System.out.println("Result :" + text);
}
}
}
以下是我的Tesseract OCR转换文本文件:
出席表
*部门:@INFO。 TECH *日期:@ 18/08/2017
*年份:@THIRD *部门:@C
*主题代码:@TBGOZ
*教师代码:@ITIOOZO
@ IT001 @ITOOZ @ IT003 @ ITOO4
m5 @ IT006 @ IT007 @ IT008
@ IT009 @ 5910 m wm -
m3 @ IT014 m @ IT016
@ IT017 @ IT018 m @ 9219
@ IT021 mz @ 593 @ IT024
m @ IT026 @ IT027 @ IT028
@ IT029 @m @ IT031 W
@ IT033 m4 @ lT035 @ IT036
我想在控制台上打印输出:
Department: INFO. TECH
Year: THIRD
Division: C
Subject Code: @TBGOZ
Teacher Code:ITIOOZO
请告诉我上述问题的解决方案。
答案 0 :(得分:0)
public static void main(String[] args) {
File oldFile = new File("C:\\temp\\test2.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(oldFile))) {
StringBuilder contents = new StringBuilder();
String text;
String lineSeparator = System.getProperty("line.separator");
while ((text = reader.readLine()) != null)
{
contents.append(text).append(lineSeparator);
// System.out.println(text);
String[] parsed = text.split("\\*");
for (String p : parsed) {
if (!p.contains("Subject Code"))
p = p.replace("@", "");
if (p.startsWith("Department")) {
System.out.println(p);
} else if (p.startsWith("Year")) {
System.out.println(p);
} else if (p.startsWith("Division")) {
System.out.println(p);
} else if (p.startsWith("Subject Code")) {
System.out.println(p);
} else if (p.startsWith("Teacher Code")) {
System.out.println(p);
}
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}