Class FileLineIterator
java.lang.Object
FileLineIterator
public class FileLineIterator extends Object implements Iterator<String>
FileLineIterator provides a useful wrapper around Java's provided
BufferedReader and provides practice with implementing an Iterator.
Your solution should not read the entire file into memory at once, instead
reading a line whenever the next() method is called. See
Java's documentation for BufferedReader to learn how to construct one given
a path to a file. Then, think about how you can use BufferedReader's
methods within this class to implement our desired functionality.
Note: Any IOExceptions thrown by readers should be caught and handled properly.
-
Constructor Summary
Constructors Constructor Description FileLineIterator(String filePath)
Creates a FileLineIterator for the file located at filePath. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Constructor Details
-
FileLineIterator
Creates a FileLineIterator for the file located at filePath. Fill out the constructor so that a user can instantiate a FileLineIterator. Feel free to create and instantiate any variables that your implementation requires here. See recitation and lecture notes for guidance.If an IOException is thrown by the BufferedReader or FileReader, then hasNext should return false.
- Parameters:
filePath
- - the path to the CSV file to be turned to an Iterator- Throws:
IllegalArgumentException
- if filePath is null or if the file doesn't exist
-
-
Method Details
-
hasNext
public boolean hasNext()Returns true if there are lines left to read in the file, and false otherwise.If there are no more lines left, this method should close the BuffereReader.
-
next
Returns the next line from the file, or throws a NoSuchElementException if there are no more strings left to return (i.e. hasNext() is false).This method also advances the iterator in preparation for another invocation. If an IOException is thrown during a next() call, the next time next() is called, it should throw a NoSuchElementException.
- Specified by:
next
in interfaceIterator<String>
- Returns:
- the next line in the file
- Throws:
NoSuchElementException
- if there is no more data in the file
-