Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

public void loadFromFile(String filename) {
    File file = new File(filename);
    BufferedReader br;

    try {
        br = new BufferedReader(new FileReader(file));
        numberOfAttributes = Integer.parseInt(br.readLine());
    } 
    ...
}

Above is my program: I am trying to read from a txt file where the first line is the number 22 and nothing more. I don't know why the program gives me an exception.

like image 927
ToBeGeek Avatar asked Dec 08 '22 16:12

ToBeGeek


1 Answers

Try stripping any whitespace from the string:

        numberOfAttributes = Integer.parseInt(br.readLine().trim());
like image 194
James Scholes Avatar answered Jan 07 '23 22:01

James Scholes