Alberto Santos Araus
Alberto Santos Araus

Reputation: 51

What am I missing? NumberFormatException error

I want to read from a txt file which contains just numbers. Such file is in UTF-8, and the numbers are separated only by new lines (no spaces or any other things) just that. Whenever i call Integer.valueOf(myString), i get the exception.

This exception is really strange, because if i create a predefined string, such as "56\n", and use .trim(), it works perfectly. But in my code, not only that is not the case, but the exception texts says that what it couldn't convert was "54856". I have tried to introduce a new line there, and then the error text says it couldn't convert "54856 " With that out of the question, what am I missing?

File ficheroEntrada = new File("C:\\in.txt");
FileReader entrada =new FileReader(ficheroEntrada);
BufferedReader input = new BufferedReader(entrada);

String s = input.readLine();
System.out.println(s);
Integer in;
in = Integer.valueOf(s.trim());
System.out.println(in);

The exception text reads as follows:

Exception in thread "main" java.lang.NumberFormatException: For input string: "54856"
    at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:68)
    at java.base/java.lang.Integer.parseInt(Integer.java:658)
    at java.base/java.lang.Integer.valueOf(Integer.java:989)
    at Quicksort.main(Quicksort.java:170)

The file in.txt consists of:

54856
896
54
53
2
5634

Upvotes: 3

Views: 509

Answers (4)

MS90
MS90

Reputation: 1249

Most probably you have a leading/trailing whitespaces in your input, something like:

String s = " 5436";
System.out.println(s);
Integer in;
in = Integer.valueOf(s.trim());
System.out.println(in);

Use trim() on string to get rid of it.

UPDATE 2:

If your file contains something like:

54856\n
896
54\n
53
2\n
5634

then use following code for it:

  ....your code
    FileReader enter = new FileReader(file);
    BufferedReader input = new BufferedReader(enter);
    String currentLine;
    while ((currentLine = input.readLine()) != null) {
    Integer in;
    //get rid of non-numbers
    in = Integer.valueOf(currentLine.replaceAll("\\D+",""));
    System.out.println(in);
    ...your code

Upvotes: 0

Ashishkumar Singh
Ashishkumar Singh

Reputation: 3600

Try reading the file with Scanner class has use it's hasNextInt() method to identify what you are reading is Integer or not. This will help you find out what String/character is causing the issue

public static void main(String[] args) throws Exception {
    File ficheroEntrada = new File(
            "C:\\in.txt");
    Scanner scan = new Scanner(ficheroEntrada);
    while (scan.hasNext()) {
        if (scan.hasNextInt()) {
            System.out.println("found integer" + scan.nextInt());
        } else {
            System.out.println("not integer" + scan.next());
        }
    }
}

Upvotes: 1

Alberto Santos Araus
Alberto Santos Araus

Reputation: 51

Well, aparently it had to do with Windows and those \r that it uses... I just tried executing it on a Linux VM and it worked. Thanks to everyone that answered!!

Upvotes: 2

TreffnonX
TreffnonX

Reputation: 2930

If you want to ensure parsability of a string, you could use a Pattern and Regex that.

Pattern intPattern = Pattern.compile("\\-?\\d+");
Matcher matcher = intPattern.matcher(input);
if (matcher.find()) {
    int value = Integer.parseInt(matcher.group(0));
    // ... do something with the result.
} else {
    // ... handle unparsable line.
}

This pattern allows any numbers and optionally a minus before (without whitespace). It should definetly parse, unless it is too long. I don't know how it handles that, but your example seems to contain mostly short integers, so this should not matter.

Upvotes: 0

Related Questions