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

Create a method called ReadTextFile. The method should return a List of strings

ID: 3830653 • Letter: C

Question

Create a method called ReadTextFile. The method should return a List of strings and does not require any arguments (has no parameters). In the method, open a text file using the file information stored in pre-defined constant named FILE_PATH. Don't worry about declaring the constant, just assume it has been declared. Also, you can assume that System.IO has been added as a using directive. In the method, read the contents of the file and store each line in the List. After all the lines have been read, return the list. Don't forget to use a try/catch block.

Explanation / Answer

Please find my implementation.

##############

import java.io.File;

import java.io.FileNotFoundException;

import java.util.NoSuchElementException;

import java.util.Scanner;

public class ReadTextFile

{

   private Scanner input;

   // enable user to open file

   public void openFile()

   {

try

{

   Scanner tempScanner = new Scanner( System.in );

   System.out.println(

"Enter the name of the file containing the student records:" );

   String fileName = tempScanner.nextLine();

   input = new Scanner( new File( fileName ) );

} // end try

catch ( FileNotFoundException fileNotFoundException )

{

   System.err.println( "Error opening file." );

   System.exit( 1 );

} // end catch

   } // end method openFile

   // read record from file

   public void readRecords()

   {

// object to be written to screen

Student record = new Student();

double total = 0; // stores total of all grades

int gradeCounter = 0; // counts grades input

System.out.printf( " %-12s%-12s%-12s%10s ", "Student ID",

   "First Name", "Last Name", "Grade" );

try // read records from file using Scanner object

{

   while ( input.hasNext() )

   {

record.setStudentID( input.nextInt() ); // read account number

record.setFirstName( input.next() ); // read first name

record.setLastName( input.next() ); // read last name

record.setGrade( input.nextDouble() ); // read balance

// display record contents

System.out.printf( "%-12d%-12s%-12s%10.2f ",

   record.getStudentID(), record.getFirstName(),

   record.getLastName(), record.getGrade() );

++gradeCounter;

total += record.getGrade();

   } // end while

   System.out.printf(

" Class average is: %.2f ", ( total / gradeCounter ) );

} // end try

catch ( NoSuchElementException elementException )

{

   System.err.println( "File improperly formed." );

   input.close();

   System.exit( 1 );

} // end catch

catch ( IllegalStateException stateException )

{

   System.err.println( "Error reading from file." );

   System.exit( 1 );

} // end catch

   } // end method readRecords

   // close file and terminate application

   public void closeFile()

   {

if ( input != null )

   input.close(); // close file

   } // end method closeFile

} // end class ReadTextFile

################# Test Class #############

public class ReadTextFileTest

{

   public static void main( String args[] )

   {

ReadTextFile application = new ReadTextFile();

application.openFile();

application.readRecords();

application.closeFile();

   } // end main

} // end class ReadTextFileTest

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