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

Put a method that will read each word from the input file and put it into a Stri

ID: 3550139 • Letter: P

Question

 Put a method  that will read each word from the input file and put it into a String array in the following code.  import java.io.BufferedReader; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.IOException;  public class TextFileInput  {      private String filename;     private BufferedReader br;       private int lineCount = 0;    public TextFileInput(String filename)    {       this.filename = filename;       try  {          br = new BufferedReader(                   new InputStreamReader(                       new FileInputStream(filename)));       } catch ( IOException ioe )  {          throw new RuntimeException(ioe);       }  // catch    }  // constructor     public void close()    {       try  {          br.close();          br = null;       } catch ( NullPointerException npe )  {          throw new NullPointerException(                         filename + "already closed.");       } catch ( IOException ioe )  {          throw new RuntimeException(ioe);       }  // catch    }  // method close      public String readLine()    {       return readLineOriginal();    }  // method readLine()     public int getLineCount()  { return lineCount; }     public static boolean isOneOf(char toBeChecked,                                  char[] options)    {       boolean       for ( int i = 0; i < options.length && !oneOf; i++ )          if ( Character.toUpperCase(toBeChecked)                    == Character.toUpperCase(options[i]) )                   return oneOf;    }  // method isOneOf(char, char[])       public static boolean isOneOf(String toBeChecked,                                  String[] options)    {       boolean       for ( int i = 0; i < options.length && !oneOf; i++ )          if ( toBeChecked.equalsIgnoreCase(options[i]) )                   return oneOf;    }  // method isOneOf(String, String[])      public String readSelection(String[] options) 
    {       if ( options == null || options.length == 0 )          throw new NullPointerException(                             "No options provided for "                             + " selection to be read in file "                             + filename + ", line "                              + (lineCount + 1) + ".");        String answer = readLine();        if ( answer == null )          throw new NullPointerException(                             "End of file "                             + filename + "has been reached.");        if ( !TextFileInput.isOneOf(answer, options) )  {          String optionString = options[0];          for ( int i = 1; i < options.length; i++ )             optionString += ", " + options[i];          throw new RuntimeException("File " + filename                             + ", line " + lineCount                             + ": "" + answer                             + "" not one of "                             + optionString + ".");       }  // if       return answer;   }  // method readSelection        public boolean readBooleanSelection()    {       String[] options = {"Y", "N", "yes", "no", "1", "0",                           "T", "F", "true", "false"};       String answer = readSelection(options);       return isOneOf(answer,                      new String[] {"Y", "yes", "1", "T", "true"} );    }  // method askUserYesNo     protected final String readLineOriginal()    {        try  {           if ( br == null )              throw new RuntimeException(                                 "Cannot read from closed file "                                 + filename + ".");           String line = br.readLine();           if ( line != null )              lineCount++;           return line;        } catch (IOException ioe)  {           throw new RuntimeException(ioe);        }  // catch    }  // method readLineOriginal }  // class TextFileInput

Explanation / Answer

I've added my method towards the end of your program, pls have a look.

Comment out any one approach while running the program. Both running at same time may cause problems



import java.io.BufferedReader;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.IOException;


public class TextFileInput {


private String filename;


private BufferedReader br;


private int lineCount = 0;


private String[] fileData;


public TextFileInput(String filename)

{

this.filename = filename;

try {

br = new BufferedReader(

new InputStreamReader(

new FileInputStream(filename)));

} catch ( IOException ioe ) {

throw new RuntimeException(ioe);

} // catch

} // constructor


public void close()

{

try {

br.close();

br = null;

} catch ( NullPointerException npe ) {

throw new NullPointerException(

filename + "already closed.");

} catch ( IOException ioe ) {

throw new RuntimeException(ioe);

} // catch

} // method close



public String readLine()

{

return readLineOriginal();

} // method readLine()


public int getLineCount() { return lineCount; }


public static boolean isOneOf(char toBeChecked,

char[] options)

{

boolean>

for ( int i = 0; i < options.length && !oneOf; i++ )

if ( Character.toUpperCase(toBeChecked)

== Character.toUpperCase(options[i]) )

oneOf = true;

return oneOf;

} // method isOneOf(char, char[])



public static boolean isOneOf(String toBeChecked,

String[] options)

{

boolean>

for ( int i = 0; i < options.length && !oneOf; i++ )

if ( toBeChecked.equalsIgnoreCase(options[i]) )

oneOf = true;

return oneOf;

} // method isOneOf(String, String[])


public String readSelection(String[] options)

{

if ( options == null || options.length == 0 )

throw new NullPointerException(

"No options provided for "

+ " selection to be read in file "

+ filename + ", line "

+ (lineCount + 1) + ".");


String answer = readLine();


if ( answer == null )

throw new NullPointerException(

"End of file "

+ filename + "has been reached.");


if ( !TextFileInput.isOneOf(answer, options) ) {

String optionString = options[0];

for ( int i = 1; i < options.length; i++ )

optionString += ", " + options[i];

throw new RuntimeException("File " + filename

+ ", line " + lineCount

+ ": "" + answer

+ "" not one of "

+ optionString + ".");

} // if

return answer;

} // method readSelection



public boolean readBooleanSelection()

{

String[] options = {"Y", "N", "yes", "no", "1", "0",

"T", "F", "true", "false"};

String answer = readSelection(options);

return isOneOf(answer,

new String[] {"Y", "yes", "1", "T", "true"} );

} // method askUserYesNo


protected final String readLineOriginal()

{

try {

if ( br == null )

throw new RuntimeException(

"Cannot read from closed file "

+ filename + ".");

String line = br.readLine();

if ( line != null )

lineCount++;

return line;

} catch (IOException ioe) {

throw new RuntimeException(ioe);

} // catch

} // method readLineOriginal


/**

* Reading a file completely depends on in which format

* data is written to it. Is every word in a new Line or they are just

* separated by a space as in paragraphs.

* Since you didn't mention, I will take both scenarios.

*/

public void storeFileDataToArray(){

/*-***********when every word is in new line**********************/

try{

String line;

int arraySize = 0;

while(br.readLine()!=null){

arraySize++;

}

fileData = new String[arraySize];

int pos = 0;

while((line=br.readLine())!=null){

fileData[pos] = line;

pos++;

}

}

catch(IOException ioe){

System.err.println("File not found or some other IO Excepiton::"+ioe);

}

/*-***********while the file is just like a paragraph***************/

StringBuilder fileDataInOneLine = new StringBuilder();

try{

String line;

while((line=br.readLine())!=null){

fileDataInOneLine.append(line);

}

// append all the file data in a single string and slit in to an array

// Since StringBuilder is mutable, Its use is more efficient than

//use of string

fileData = fileDataInOneLine.toString().split(" ");

}

catch(Exception e){

System.err.println("Some exception occured"+e);

}

}




} // class TextFileInput

Dr Jack
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote