Note that there are some explanatory texts on larger screens.

plurals
  1. POTrying 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
    primarykey
    data
    text
    <p>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. And that works great. I can print out the strings &amp; chars however I want. But when I try to parse those strings and chars I get the following error: </p> <pre><code>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) </code></pre> <p>From what I have found with google I think the error is located in how I read my file.</p> <pre><code>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; } } </code></pre> <p>And for the parsing bit</p> <pre><code>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&lt;test2[0].length(); i++) { char tmp = test.charAt(i); String S = Character.toString(tmp).trim(); //int i1 = Integer.parseInt(S); System.out.print(S); } </code></pre> <p>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:</p> <pre><code>12112211 </code></pre> <p>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 <code>""</code>. So how can I remove the <code>""</code>? 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. </p> <p>Thanks in advance :)</p> <p>Yeah, and another thing: If I replace <code>""</code> with <code>"0"</code> it works, but then I get all those zeros which I can't find a clever way to remove. 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. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. COFor each character in the first line of your file, you create a string consisting of that one character, then you "trim" the string (removing leading and trailing whitespace -- that is: you change `" "` or `"\t"` to `""`), then you parse that string as an integer. So if you're getting an exception that says your string is empty, this means that you had a whitespace character somewhere in the first line of your file. The solution is -- don't have any whitespace characters anywhere in the first line of your file.
      singulars
    2. COSo you are saying that it will be impossible to do this with whitespace in the first line? because the files I obtained are for homework and I do not think I can (or should) edit them :/
      singulars
    3. COI'm saying that *your current code* will not work if there's whitespace in the first line. (I wasn't very serious when I said that the solution is not to have any whitespace characters anywhere in the first line of your file. In reality, you need, firstly, to understand *why* your program is failing -- did you understand my explanation? -- and secondly, to change your program to support whatever data it needs to support.)
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload