-2

EDIT: Simple fix installed with a check for a blank line. Chalk this up to me not fully understanding I/O yet and how the read.line() works.

Final code:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class MagicSquareAnalysis {
public static boolean testMagic(String pathName) throws IOException {
    // Open the file
    BufferedReader reader = new BufferedReader(new FileReader(pathName));

    boolean isMagic = true;
    int lastSum = -1;

    // For each line in the file ...
    String line;
    while ((line = reader.readLine()) != null) {
        // ... sum each row of numbers
        String[] parts = line.split("\t");
        int sum = 0;
        for (String part : parts) {
            if (line.isEmpty()) {
                continue;
            } else {
            sum += Integer.parseInt(part);
            }
        }

        if (lastSum == -1) {
            // If this is the first row, remember the sum
            lastSum = sum;
        } else if (lastSum != sum) {
            // if the sums don't match, it isn't magic, so stop reading
            isMagic = false;
            break;
        }
    }

    reader.close();
    return isMagic;
}

public static void main(String[] args) throws IOException {
    String[] fileNames = {     "C:\\Users\\Owner\\workspace\\MagicSquares\\src\\Mercury.txt", "C:\\Users\\Owner\\workspace\\MagicSquares\\src\\Luna.txt" };
    for (String fileName : fileNames) {
        System.out.println(fileName + " is magic? " + testMagic(fileName));
    }
}
}

Original problem

Have a Java program that reads a .txt file of numbers. The text file has several rows of numbers spaced out by a tab. I continued to receive an error with line 22 of my program, which was this code:

sum += Integer.parseInt(part);

After a few statements to check the progress of the program, I discovered the error was occurring after the program analyzed the first line. For some reason the input it kept trying to read was a "" in the blank line. The read.line() did not seem to be skipping the blank line as it should have with the while statement.

Any ideas why the program is not skipping over the blank line and trying to read it regardless?

Mistweaver
  • 25
  • 1
  • 7
  • `The read.line() did not seem to be skipping the blank line as it should have with the while statement.` Your understanding of why readLine() returns null is incorrect. [Re-read the Javadoc](http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html) - it returns null when EOF is reached. – Greg Kopff Mar 14 '14 at 05:19

3 Answers3

1

Because split, as per the docs, is going to return an array containing a single element which is an empty string.

readline() is stripping the \n (or \r\n on windows) from the line, returning an empty string.

When you do:

String[] parts = line.split("\t");

There's an empty leading string when the regular expression is executed against the empty string, causing parts[0] to be ""

You can see this via a simple test:

public static void main( String[] args )
{
    String foo = "";
    System.out.println("String length: " + foo.length());
    String[] parts = foo.split("\t");
    System.out.println("Array Length: " + parts.length);
    System.out.println("Length of that one String in the array: " + parts[0].length());
}

Output:

String length: 0
Array Length: 1
Length of that one String in the array: 0

At the very least you need to check for that empty string:

for (String part : parts) {
    if (!part.isEmpty()) {
        sum += Integer.parseInt(part);
    }
}
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
0

You can try this:

while ((line = reader.readLine()) != null) {
    if ( line.trim().length() == 0)
         continue;
    //Your other code
}
Mohammad Ashfaq
  • 1,333
  • 2
  • 14
  • 38
0

In string "" is different from null check this link for more idea. Difference between null and empty ("") Java String

Also readline() returns null only when EOF(end of file) is reached.

While doing split("\t") you might get "" so better to check the string for "" before parse it to a number.

for (String part : parts) {
if(!part.isEmpty()){
//your code
}}
Community
  • 1
  • 1
Lakshmi
  • 2,204
  • 3
  • 29
  • 49