我试图通过另一种语言[sinhala]读取一个包含英语单词和含义的文本文件,这些语言用相同的标记分隔并用" |"标记。它看起来像:
abmodality=අපමාතිය
abnegate=අයිතිවාසිකම අත්හරිනවා | ත්යජනය කරනවා | පිළිගැනීම අත්හරිනවා | මතය අත්හරිනවා
abnormal=අනියම් | අපසාමාන්ය | අප්රමත | අවප්රමාණ | අස්වාභාවික | අසාමාන්ය | පුදුම
abnormalism=අසාමාන්යභාවය
abnormality=අනියම් බව | අප්රමතිය | අසාමාන්යත්වය | අසාමාන්යය | විපරීතය | විශේෂය
abnormally=අනියම් ලෙසින් | අසාමාන්ය ලෙස | රියකට ගොඩ වී | විපරීතව
aboard=දුම්රියේ | නැවේ | යාත්රාවකට ගොඩ වී | යාත්රාවේ | වාහනයක
实际上这是字典文件,它包含说明a到z的所有英文单词。我将使用此文件创建MySQL数据库,因为我无法通过手动插入所有单词和含义&# 39;太大了。
所以我创建了java程序,它以编程方式创建MySQL查询,所以我可以轻松地插入我的1到多个数据库,其中有2个表。实际上我的问题是程序给我一个错误,我发现哪行文本文件给出了这个错误,而阅读。重要且令人困惑的事实是它不是一个独特的行,如果我在阅读不同的行时运行我的程序5次错误。
这是我的代码
String[] parts ;
String en;
String[] sin;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("C:\\Users\\Madhawa.se\\Desktop\\bac\\gui\\lktips-v3-lionlk.com\\a.txt"), "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String s = br.readLine();
parts = s.split("=");
en=parts[0];
sin=parts[1].split(" | ");
sb.append("INSERT INTO `singlish`.`eng` (`eid`, `eword`) VALUES ('"+stpoint+"', '"+en+"');")
sb.append(System.lineSeparator());
System.out.println(en);
for(int i=0;i<sin.length;i++){
sb.append("INSERT INTO `singlish`.`sng` (`eid`, `sword`) VALUES ('"+stpoint+"', '"+sin[i]+"');");
sb.append(System.lineSeparator());
}
System.out.println(""+stpoint);
stpoint++;
}
System.out.println("finished"+subs.size());
String everything = sb.toString();
jTextArea1.append(everything);
br.close();
} catch (Exception ex) {
ex.printStackTrace();
}
然而这是我得到的输出和错误,但每次运行时结果都会改变
attention
3742
attentive
3743
attentively
3744
java.lang.NullPointerException
at subtitleseeker.NewJFrame.jButton3ActionPerformed(NewJFrame.java:180)
at subtitleseeker.NewJFrame.access$200(NewJFrame.java:17)
at subtitleseeker.NewJFrame$3.actionPerformed(NewJFrame.java:60)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
attentiveness
3745
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
attenuant
答案 0 :(得分:3)
在原始代码中,您从文件中读取一行并将其分配给line
变量。您还检查以确保在while循环的条件中行变量不为null。但是,您永远不会更新line
变量的值,而只是在循环内部的s
变量中读取新行,从不检查s
是否为空。< / p>
这是您的原始代码:
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
String s = br.readLine();
parts = s.split("=");
en=parts[0];
sin=parts[1].split(" | ");
sb.append("INSERT INTO `singlish`.`eng` (`eid`, `eword`) VALUES ('"+stpoint+"', '"+en+"');")
sb.append(System.lineSeparator());
System.out.println(en);
for(int i=0;i<sin.length;i++) {
sb.append("INSERT INTO `singlish`.`sng` (`eid`, `sword`) VALUES ('"+stpoint+"', '"+sin[i]+"');");
sb.append(System.lineSeparator());
}
System.out.println(""+stpoint);
stpoint++;
}
这三行:
String line = br.readLine();
while (line != null) {
String s = br.readLine();
可以替换为这一行:
for (String s; (s = br.readLine()) != null; ) {
所以你的代码是:
StringBuilder sb = new StringBuilder();
for (String s; (s = br.readLine()) != null; ) {
parts = s.split("=");
en=parts[0];
sin=parts[1].split(" | ");
sb.append("INSERT INTO `singlish`.`eng` (`eid`, `eword`) VALUES ('"+stpoint+"', '"+en+"');")
sb.append(System.lineSeparator());
System.out.println(en);
for(int i=0;i<sin.length;i++) {
sb.append("INSERT INTO `singlish`.`sng` (`eid`, `sword`) VALUES ('"+stpoint+"', '"+sin[i]+"');");
sb.append(System.lineSeparator());
}
System.out.println(""+stpoint);
stpoint++;
}