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

Effectively use the String, Character, and StringBuilder classes and associated

ID: 2989186 • Letter: E

Question

Effectively use the String, Character, and StringBuilder classes and associated methods
Write Java code to read and write data to and from a file
Deliverables:

Design, develop, test and document a Java application that reads data from an input file, processes the data, and then writes the processed data to an output file

Specific functional requirements for this project:

The application should be run from the command line using command line arguments to provide the input and output filenames.
//The application should display an appropriate error message if the user forgets to enter the input and output filenames at the command prompt or if the files are not found.
//The application should open the file and read each line of the file.
//For each line read, the application should determine the total number of characters in the line (including white-space characters), determine the number of times the number 4 appears in the line, replace all occurrences of the string "this" with "That", and reverse the order of the characters in the line.
//The output file should contain the same number of lines as the input file with the following content for each line:
//Total number of characters (including white-space characters) in the line
//Number of times the number 4 appeared in the line
//Reversed string (for example, cats in the input file would be displayed as stac)
//The items on each line in the output file should be separated by commas (for example: for input line "this4cats", the output line should be "9,1,stac4tahT")
//
//6.The application should close any open files before exiting.

Explanation / Answer

public class JFile {

public static void main(String[] args) throws IOException {

if( args.length != 2 ){

System.out.println("Invalid no of Args:");

System.out.println("Usage: JFile <input file> <output file>");

return;

}

BufferedReader br = null;

BufferedWriter bw = null;

  

try {

String path = System.getProperty("user.dir");

br = new BufferedReader(new FileReader( path + "\"+ args[0]));

bw = new BufferedWriter(new FileWriter( path + "\" +args[1]) );

} catch (IOException e) {

System.out.println( "Unable to open file: " + e.toString());

return;

}

String line;

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

int Cnt4=0;

line = line.replaceAll("this", "That");

for( int i=0; i<line.length(); i++)

if( Character.isDigit(line.charAt(i)) )

if ( line.charAt(i) == '4')

Cnt4++;

StringBuilder s = new StringBuilder();

s.append(line);

s.reverse();

s.insert(0, Cnt4 + ", ");

s.insert(0, line.length() + ", ");

bw.write(s.toString() + " ");

System.out.println(s.toString());

}

try {

if (br != null)br.close();

if( bw!=null ) bw.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

  

}

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