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

Read the \"reverse.txt\" file, one line at a time. Print the line reversed. Use

ID: 3552088 • Letter: R

Question

Read the "reverse.txt" file, one line at a time.

Print the line reversed.

Use arraylists.

you cannot use the collections "reverse" method, create your own or put it in code inside a loop.

HINT: use ".add" to re-arrange elements in an arraylist

10 bonus points: use a two dimensional ArrayList of String to read the entire file at once, instead of one line at a time.



import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;


/*
Open the reverse.txt file

*/
public class ReverseFileContents
{
   public static void main(String[] args) throws FileNotFoundException
   {
//
//
//You need to open the "reverse.txt" file here
//
//
//
         while ( inputFile.hasNextLine())
         {
//
//Inside the while loop you will get the line of text
//   one line at a time.
//
//Next, you will reverse the line and print out the "string"
//   of characters on the screen for each line
//   to reveal the ACM code of ethics.
//
         }         
   }
}

Explanation / Answer

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.PrintWriter;
  4. import java.util.*;

  5. class ReverseFileContents
  6. {
  7. public static void main(String[] args) throws FileNotFoundException
  8. {
  9. Scanner inputFile = new Scanner(new File("reverse.txt"));
  10. ArrayList<String> list = new ArrayList<String>();
  11. while(inputFile.hasNext())
  12. {
  13. list.add(inputFile.nextLine());
  14. String s = list.get(list.size()-1);
  15. for(int i=s.length()-1; i>=0; i--)
  16. System.out.print(s.charAt(i)+" ");
  17. System.out.println();
  18. }
  19. inputFile.close();
  20. }
  21. }
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