Reputation: 85
I am learning Java. I believe I have an issue understanding how BufferedReader processes "\n" or "" strings (newline and empty strings).
If I run the following it will fail if I put either of those strings into the String array.
String [] strings = {"55", "23", ""};
int total = 0;
for (String str : strings)
{
if (str != null) {
total += Integer.valueOf(str);
}
}
System.out.println(total);
This is fine, and makes sense to me. What does not make sense to me is when I run this code in reading in a file.
BufferedReader reader = null;
int total = 0;
try {
reader = new BufferedReader(new FileReader("E:\\Testing\\Numbers.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
total += Integer.valueOf(line);
System.out.println("Total: " + total);
} catch(Exception e){
System.out.println(e.getMessage());
} finally {
try {
if (reader != null)
reader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
using a text file that has the following:
5
2
3
It runs without errors. If I add a single blank line in the same file (), it fails with the message For input string: ""
I added an isNumeric function to solve the issue, but I don't understand why the BufferedReader will work when I run the code without any empty lines, even though it does not like the "\n" or "" strings. I looked up valueOf() in the javadocs and I did not see anything that helped me.
Here is my final code that uses the isNumeric function and shows how it sees both the "\n" and "" strings as non-numeric.
BufferedReader reader = null;
int total = 0;
try {
reader = new BufferedReader(new FileReader("E:\\Testing\\Numbers.txt"));
String line = null;
while ((line = reader.readLine()) != null) {
if (isNumeric(line))
{
System.out.println(line);
total += Integer.valueOf(line);
}
System.out.println("Skipping a non numeric value");
}
System.out.println("Total: " + total);
} catch(Exception e){
System.out.println(e.getMessage());
} finally {
try {
if (reader != null)
reader.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
public static boolean isNumeric(String str)
{
try
{
int d = Integer.parseInt(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
6
Skipping a non numeric value
1
Skipping a non numeric value
Skipping a non numeric value
2
Skipping a non numeric value
62
Skipping a non numeric value
23
Skipping a non numeric value
Total: 94
Finally I did see this article on the site, and it is close, but I still could not figure it out.
Upvotes: 2
Views: 2270
Reputation: 140427
When using a BufferedReader, the readLine() method will consume any "new line like" characters automatically.
So, in essence, your initial file was
5\n ...
And the \n is simply removed before giving the string to your code. If the line is just \n; then you get "". An easy way to check for that is line.isEmpty()
.
Regarding: I don't understand why the BufferedReader will work when I run the code without any empty lines; well I don't understand that question. If your code only reads lines with numbers, it doesn't matter that you have code sitting there that could deal with empty lines; or lines containing "invalid" number text.
Upvotes: 2