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

Java Write a program that reads in a file called ‘input.txt’ and prints out ever

ID: 3778075 • Letter: J

Question

Java

Write a program that reads in a file called ‘input.txt’ and prints out every other word to a file called ‘output.txt’.

You will embed the input/output names for the files in the source code.

You can create the input file with Notepad.

The path assumed is where ever the java class code is.

If using Eclipse, put the text files under the folder with the project name.

A sample run:

(Before run)

input.txt

Why's that plane dusting crops where there ain't no crops?

(After run)

output.txt

Why's plane crops there no

Looking at the sample run above, all that was done from the command line was display the text files before and after the run.

Do not prompt for the file name.

I have some of the code done but it doesn't work. Any help improving or getting the code to work will be appreciated

import java.io.*;

import java.util.*;

import java.util.Scanner;

public class TextProgram {

   public static void main(String[] args)

   {

       try{

       FileReader fr = new FileReader ("input.txt");

       FileWriter fw = new FileWriter ("output.txt");

       int c = fr.read();

           while (c!= -1);

       {

           for (int i = 0; i <= read.nextLine(); i = i+2)

           {   

               fw.write(c);

           } //end for

       } //end while

       }   //end try   

       catch (IOException e){

       System.out.println(e);

       }

       finally() {

           fr.close();

           fw.close();

       }

   } //end main

} //end class

Explanation / Answer

TextProgram.java


import java.io.*;
import java.util.*;
public class TextProgram {

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

Scanner scan = new Scanner (new File("input.txt"));
FileWriter fw = new FileWriter (new File("output.txt"));
while(scan.hasNext()){
   fw.write(scan.next()+" ");
   scan.next();
}
fw.flush();
fw.close();
scan.close();
System.out.println("File has been generated.");
  
} //end main


}

Output:

File has been generated.

output.txt

Why's plane crops there no

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