3

"out.txt" content is

1

My JAVA code is like this.

    int val=0;
    BufferedReader br = new BufferedReader(new FileReader("out.txt"));

    while(true) {
        String line = br.readLine();
        if (line==null) break;
        val=Integer.parseInt(line);
    }
    br.close();

and debugger says,

java.lang.NumberFormatException: For input string: "1"

i need to use 1 from "out.txt". How can i use 1 as Integer? Thank you for your attention :)

2
  • Look at if there are any hidden special characters, white spaces etc. Commented Mar 25, 2016 at 7:37
  • there were no any hidden special characters :) but thank you to answer me Commented Mar 25, 2016 at 7:46

4 Answers 4

11

You must be having the trailing whitespaces. Use the trim() function as follows:

val = Integer.parseInt(line.trim());

Here is the code snippet:

int val = 0;
BufferedReader br = new BufferedReader(new FileReader("out.txt"));

String line = null;
while(true) {
    line = br.readLine();
    if (line == null) break;
    val = Integer.parseInt(line.trim());
}
br.close();

Also, if you want to check whether String is null or empty you can start using the Apache Commons StringUtils as follows:

if (StringUtils.isEmpty(line)) break;
Sign up to request clarification or add additional context in comments.

1 Comment

I tried 'trim()' first but it didn't work ^^;; but thank you to answer me
9

I removed this invisible Unicode character and now parsing to INT works great. I know it's not the case here but maybe can help some other people.

text = text.replaceAll("\\uFEFF", "");

1 Comment

Yep, this is the BOM. I had the exact same problem as the OP and it turned out I had exported a CSV file from Excel as UTF-8 WITH BOM. Java treated the BOM as part of the input. I re-exported to a different encoding and the problem was solved. BOM on WikiPedia
-1

Did you create the file on a Mac? Probably there is some hidden character. You can create it on Windows and try again.

Comments

-2

Oops. Yesterday the following code worked well so i uploaded it as you are seeing. but today, i tried compile one more time without '.toString()' and well, it worked too. So It was my mistake and I'm sorry to bother you guys. (I don't know why it didn't work without '.toString' exactly the day before yesterday.)


Thank you so much people who cared of my question but i found the answer(i don't know how to find it by myself)

following codes work very well :) Thank you!

    BufferedReader br = new BufferedReader(new FileReader("out.txt"));
    int val=0;

    while(true) {
        String line = br.readLine();
        if (line==null) break;
        val=Integer.parseInt(line.toString());
    }
    br.close();

2 Comments

line is already a String! What do you think will happen if you do toString() on it?
This doesnt make sense

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.