简体   繁体   English

尝试将文件中的字符串解析为int。 获取NumberFormatException:对于输入字符串“”,eaven字符串似乎是一个很好的int字符串。 爪哇

[英]Trying to parse a string to int from a file. Get NumberFormatException: For input string: “”, eaven tho string appears to be an good int string. Java

I have been trying to figure this out for couple of hours now and I hope one of you can help me. 我已经尝试解决了几个小时,希望你们中的一个能帮助我。 I have an file (actually two but thats not important) that have some rows and columns with numbers and blank spaces between. 我有一个文件(实际上是两个,但那并不重要),其中包含一些行和列,其中包含数字和空格。 And I'm trying to read those with BufferedReader. 我正在尝试使用BufferedReader阅读这些内容。 And that works great. 而且效果很好。 I can print out the strings & chars however I want. 我可以根据需要打印出字符串和字符。 But when I try to parse those strings and chars I get the following error: 但是,当我尝试解析这些字符串和字符时,出现以下错误:

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)

From what I have found with google I think the error is located in how I read my file. 从我在google上找到的内容来看,我认为错误在于我读取文件的方式。

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;
    }
}

And for the parsing bit 而对于解析位

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);        
}

This isn't the actual code for what I planning to do with the output, but the error appears at the code that is commented out. 这不是我打算使用输出执行的实际代码,但是错误出现在注释掉的代码上。 So my string output is as following: 所以我的字符串输出如下:

12112211

Which seems good to parse to integer. 解析为整数似乎很好。 But it does not work. 但这行不通。 I tried to manually see what's in the char position 0 and 1, for 0 I get 1, but for 1 I get nothing aka "" . 我试图手动查看char位置0和1中的内容,对于0我得到1,但是对于1我什么也没有得到"" So how can I remove the "" ? 那么如何删除"" I hope you guys can help me out, and let me know if you need more info. 希望大家能帮帮我,如果您需要更多信息,请告诉我。 But I think I have covered what's needed. 但是我认为我已经满足了所有需求。

Thanks in advance :) 提前致谢 :)

Yeah, and another thing: If I replace "" with "0" it works, but then I get all those zeros which I can't find a clever way to remove. 是的,还有另一件事:如果我将""替换为"0" ,那么它可以工作,但是随后我得到了所有这些零,而我找不到一个聪明的删除方法。 But is it possible to maybe skip them while parsing or something? 但是有可能在解析时跳过它们吗? My files only hold 1 and 2, so it wouldn't interfere with anything if it is possible. 我的文件仅包含1和2,因此如果可能的话,它不会干扰任何内容。

如果您有两个相邻的分割字符(即\\ n \\ n),或者如果在trim()调用中传递了空格字符,则将返回字符串“”,因此请忽略空字符串并继续。

You could use the Scanner class to parse for ints, skipping Whitespace: 您可以使用Scanner类来解析整数,跳过空白:

sc = new java.util.Scanner (line);
sc.nextInt ();

Another idea is to trim the line, split, and parse the parts: 另一个想法是修剪线,分割并解析零件:

lin = line.trim ();
String [] words = lin.split (" +");
for (String si : words) 
    Integer.parseInt (si);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM