Getting the following error while reading the contents of the file, I think that my code is not converting the it into integer rightly following are the errors:
Exception in thread "main" java.lang.NumberFormatException: For input string: "3 "
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at Program.main(Program.java:192)
and here is the code:
FileReader fin = new FileReader("task.txt");
BufferedReader sr = new BufferedReader (fin);
int count = 0;
int number = Integer.parseInt(sr.readLine());
You are trying to parse a String that contains a space and a digit - "3 "
.
You should eliminate the spaces before parsing to int :
int number = Integer.parseInt(sr.readLine().trim());