Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class named Lines that reads and returns one line at a time from a file.

ID: 3910633 • Letter: W

Question

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

You have not mentioned programming language. Based on description, assuming it to be java. Along with the Lines class, I have also given a test program to use it.
I have also given a sample input file data.txt which I used for testing.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you
NOTE: Please make sure you place your input file in the correct folder. If using eclipse, the file should be in the project directly and NOT INSIDE src folder.


input file: data.txt
--------
Country|Population|Area
China|1,339,190,000|9,596,960.00
United States of America|309,975,000|9,629,091.00
Brazil|193,364,000|8,511,965.00
Japan|127,380,000|377,835.00
Canada|34,207,000|9,976,140.00
Indonesia|260,581,100|1,809,590.97
Nigeria|186,987,563|912,134.45
Mexico|128,632,004|1,969,230.76
Egypt|93,383,574|1,000,000.00
France|64,668,129|541,656.76
Italy|59,801,004|300,000.00
South Africa|54,978,907|1,222,222.22
South Korea|50,503,933|98,076.92
Colombia|48,654,392|1,090,909.09


Lines.java
-----------
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Lines {
private Scanner input = null;
public Lines(String filename){
try {
input = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}

public String getLine(){
if(input == null) //no more lines or file does not exist
return null;

if(input.hasNextLine()){
return input.nextLine();
}
else { //no more lines
input.close();
input = null;
return null;
}

}

}

TestLines.java
---------------
import java.util.Scanner;

public class TestLines {

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
String filename;

System.out.print("Enter input filename: ");
filename = keyboard.nextLine();

Lines lines = new Lines(filename);
String str;

System.out.println("File contents:");
while((str = lines.getLine()) != null)
System.out.println(str);

}

}


output
----
Enter input filename: data.txt
File contents:
Country|Population|Area
China|1,339,190,000|9,596,960.00
United States of America|309,975,000|9,629,091.00
Brazil|193,364,000|8,511,965.00
Japan|127,380,000|377,835.00
Canada|34,207,000|9,976,140.00
Indonesia|260,581,100|1,809,590.97
Nigeria|186,987,563|912,134.45
Mexico|128,632,004|1,969,230.76
Egypt|93,383,574|1,000,000.00
France|64,668,129|541,656.76
Italy|59,801,004|300,000.00
South Africa|54,978,907|1,222,222.22
South Korea|50,503,933|98,076.92
Colombia|48,654,392|1,090,909.09

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote