Question 4: (Java code) please answer this as soon as possible. write comments i
ID: 3910237 • Letter: Q
Question
Question 4: (Java code) please answer this as soon as possible. write comments in program
Write a class named Lines that reads and returns one line at a time from a file. The constructor is passed the name of the file (a String) to be processed. Include a method named getLine() that returns the next line in the file each time that getLine() is called.
Return null when the end of the file is encountered. Do not read or store more than one line at a time. Do not perform any processing on each input line, return it exactly as it was stored in the input file. Do not read the contents of the input file more than once.
Explanation / Answer
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class Lines { private Scanner fin; public Lines(String filename) { try { fin = new Scanner(new File(filename)); } catch (FileNotFoundException e) { e.printStackTrace(); } } public String getLine() { if(fin.hasNextLine()) { return fin.nextLine(); } else { return null; } } }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.