"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 :)
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;