0

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 & 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.

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 "". 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. 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.

user unknown
  • 35,537
  • 11
  • 75
  • 121
dtd
  • 79
  • 11
  • For 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. – ruakh Mar 11 '12 at 22:44
  • So 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 :/ – dtd Mar 11 '12 at 23:08
  • I'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.) – ruakh Mar 11 '12 at 23:13
  • I think I got it now, the part that is failing is my for loop, because for each time it adds a new string it also adds an "" ? And the code for reading mye file is fine, and not the problem as I first thought? – dtd Mar 11 '12 at 23:27
  • Your code for reading your file is a bit strange, but I don't think it's really a problem. The problem is in the for-loop. But your statement that "for each time it adds a new string it also adds an ''" is not correct. – ruakh Mar 11 '12 at 23:44
  • Yeah, thx I think I got it now, I can just trim all the whitespace before my for loop, and the problem is resolved :) And for the reading file code, it may seem a bit strange because I have splitted it up a bit to have the option to read the two files seperatly and I didnt include all of it, also the variable names may be a bit missleading. But Im not a programming genius (yet :P) So it may in fact be a bit odd. – dtd Mar 11 '12 at 23:48

2 Answers2

0

The string "" will be returned if you have 2 of the splitting characters next to each other (i.e. \n\n) or if there is a whitespace character being passed into the trim() call so ignore empty strings and carry on.

andy.xyz
  • 2,567
  • 1
  • 13
  • 18
  • I think you're misreading. `parseInt` only gets called on single-character substrings of the first element of the array that `split` returns; so it's not a problem if `split`'s return-value contains empty strings. (Or at least, that's not *the* problem. It may be *a* problem.) – ruakh Mar 11 '12 at 22:46
  • but trim() will change a space or tab or \r to an empty string again – andy.xyz Mar 11 '12 at 22:50
  • Yes, I'm aware of that, but that's not what your answer was about. And your answer still mentions impossibilities alongside that possibility. – ruakh Mar 11 '12 at 22:55
  • Thanks for the tip about \n\n that might be a problem somewhere. But what did you mean with the trim()? because I did use trim several places, both when reading the file and when parsing to string. Are you saying that I shouldt have used trim() because it can be the problem? – dtd Mar 11 '12 at 22:57
  • The line immediately before the parseInt will convert whitespace to an empty string so if your input contains any whitespace except the newline that will cause it to break. – andy.xyz Mar 11 '12 at 23:00
  • Okey, think I got it as ruakh mentioned it, any tip to resolve the issue? – dtd Mar 11 '12 at 23:32
  • You want to strip the whitespace I guess - http://stackoverflow.com/questions/5455794/removing-whitespace-from-strings-in-java – andy.xyz Mar 11 '12 at 23:34
  • omg... I think I just got it. I can just trim all the whitespaces before I enter the string to the for loop, Well jeez thanks guys, should have figured this out by myself, guess im to tierd. Bit to many hours with eclipse today. – dtd Mar 11 '12 at 23:44
0

You could use the Scanner class to parse for ints, skipping Whitespace:

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);
user unknown
  • 35,537
  • 11
  • 75
  • 121