Keep looping

November 28, 2009

Another funny implementation of the line iterator mentioned before. Those two did not at all understand the concept of lightweight associated with an iterator. Each call of next() or hasNext() resulted in a call of this method:

public String getLineAtPosition (int index) {
    FileInputStream dataFile = new FileInputStream(this.fileName);
    byte[] buffer = new byte[1];
    String data = "";

    while ((dataFile.available() != 0) && (index != 0)) {
        dataFile.read(buffer);
        data += (char) buffer[0];
        if (lineBreak(data)) {
            index--;
            data = "";
        }
    }
    if (dataFile.available() == 0) {
        return "Insufficient data.";
    }
    while (dataFile.available() != 0) {
        dataFile.read(buffer);
        data += (char) buffer[0];
        if (lineBreak(data)) {
            data = noLineBreak(data);
            break;
        }
    }
    return data;
}

Well, there are a lot of grave mistakes in here, too. But imagine this method called every time! A test case reading Goethe’s “Zauberlehrling” (111 lines) took almost a minute to finish…


Not in use

November 28, 2009

This was handed in as an implementation of a class, which allows iterating the lines of a file one by one. Of course, the students were supposed to use the Iterator interface from the Java library.

/**
 * not in use
 */
public boolean hasNext() {
    return false;
}

I wonder if they even bothered to look at the documentation at all.


Follow

Get every new post delivered to your Inbox.