我已经尝试了几个小时来解决这个问题,我希望你们中的一个可以帮助我。我有一个文件(实际上是两个,但那并不重要),其中有一些行和列,其中有数字和空格。我正在尝试使用 BufferedReader 阅读这些内容。这很好用。我可以打印出我想要的字符串和字符。但是当我尝试解析这些字符串和字符时,我收到以下错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at FileProcess.processed(FileProcess.java:30)
at DecisionTree.main(DecisionTree.java:16)
根据我在谷歌上的发现,我认为错误在于我阅读文件的方式。
public class ReadFiles {
private BufferedReader read;
public ReadFiles(BufferedReader startRead) {
read = startRead;
}
public String readFiles() throws IOException {
try {
String readLine = read.readLine().trim();
String readStuff = "";
while(readLine != null) {
readStuff += (readLine + "\n");
readLine = read.readLine();
}
return readStuff;
}
catch(NumberFormatException e) {
return null;
}
}
对于解析位
public class FileProcess {
public String processed() throws IOException {
fileSelect fs = new fileSelect();
ReadFiles tr = new ReadFiles(fs.traning());
String training = tr.readFiles();
ReadFiles ts = new ReadFiles(fs.test());
String test = ts.readFiles();
List liste = new List(14,test.length());
String[] test2 = test.split("\n");
for(int i = 0; i<test2[0].length(); i++) {
char tmp = test.charAt(i);
String S = Character.toString(tmp).trim();
//int i1 = Integer.parseInt(S);
System.out.print(S);
}
这不是我计划对输出执行的实际代码,但错误出现在被注释掉的代码中。所以我的字符串输出如下:
12112211
解析为整数似乎很好。但它不起作用。我试图手动查看 char 位置 0 和 1 中的内容,对于 0,我得到 1,但对于 1,我什么也得不到""
。那么我怎样才能删除""
呢?我希望你们能帮助我,如果您需要更多信息,请告诉我。但我想我已经涵盖了所需的内容。
提前致谢 :)
是的,还有一件事:如果我用它替换""
它"0"
会起作用,但是我会得到所有那些我找不到聪明的方法来删除的零。但是有可能在解析时跳过它们吗?我的文件只包含 1 和 2,所以如果可能的话,它不会干扰任何东西。