Question

In: Computer Science

Write a program to reverse the lines of a file and to reverse the words plus...

Write a program to reverse the lines of a file and to reverse the words plus the letter's of each word within each line using ArrayList.

A file name mobydick.txt

Example:

Original file contains the following

MOBY DICK; OR THE WHALE

by Herman Melville


CHAPTER 1

Loomings.

Out put should be

.sgnimooL

1 RETPAHC


ellivleM namreH yb

ELAHW EHT RO ;KCID YBOM

its for java eclipse

Solutions

Expert Solution

Solution

Steps :

1.Create an empty arraylist to typee string

2.Take Input file from your system (Donot forgot to add the full address of the file in the address)

3.Iterate each line of the file and add it to the arraylist.

4.Now for each string in arraylist iterate it from back to top (length-1 to 0)

5.For every string we found after reversal our final step is to reverse the strinn using the reverse function in StringBuffer Java Class.

6.At last print to next line after each index of arraylist.

Java Prograam :

/* importing the util package*/
import java.io.*;
import java.util.*;

public class ReverseFile
{

/* main function */
public static void main (String[]args) throws FileNotFoundException
{

/* creating the arraylist for storing the words in the string */
ArrayList < String > reverseFileWords = new ArrayList <> ();

/*taking the input from the file
* you have to replace the txt file address with your own full address of the file in your system*/
//TODO
Scanner input =
new Scanner (new File ("-----replace with our own txt file URL------"));

/* iterating the file for every line */
while (input.hasNextLine ())
{
   /* adding the seperate line in the linked list */
   reverseFileWords.add (input.nextLine ());
}
/* this loop seperate the each word from the line
* we are seperating on the basics of the space*/
for (int i = (reverseFileWords.size () - 1); i >= 0; i--)
{
   String ar[] = reverseFileWords.get (i).split (" ");

   for (int j = (ar.length - 1); j >= 0; j--)
   {
   /* reversing each word by using the string buffer */
   StringBuffer buffer = new StringBuffer (ar[j]);
   System.out.print (buffer.reverse () + " ");
   }
   /* printing the nexr line */
   System.out.println (" ");
   ar = null;
}


}

}

Code screenshot.

Output ScreenShot :


Related Solutions

write a program that replace each line of a file with its reverse. for example, if...
write a program that replace each line of a file with its reverse. for example, if you run: java Reverse HelloPrinter.java then the contents of HelloPrinter.java are changed to retnirPolleH ssalc clibup { )sgra ][gnirtS(niam diov citats clibup { wodniw elosnoc eht ni gniteerg a yalpsiD // ;) "!dlroW ,olleH " (nltnirp.tuo.mestyS } }
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
IN PYTHON: Write a program that displays the lines from the total.txt file in the following...
IN PYTHON: Write a program that displays the lines from the total.txt file in the following output. Use a try catch phrase to check for errors. Use only one function for this portion [main()]. Remember to use Python. Sample output below: 19 16, 29 3, 30 4, 34
A file concordance tracks the unique words in a file and their frequencies. Write a program...
A file concordance tracks the unique words in a file and their frequencies. Write a program that displays a concordance for a file. The program should output the unique words and their frequencies in alphabetical order. Variations are to track sequences of two words and their frequencies, or n words and their frequencies.
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
Write a C program, called reverse, using standard I/O functions, to take a file as input...
Write a C program, called reverse, using standard I/O functions, to take a file as input then copies it to another file in reverse order. That is, the last byte becomes the first, the byte just before the last one becomes the second, etc. The program call should look like: reverse fileIn fileOut
2. Write a Java program that reads a series of input lines from given file “name.txt”,...
2. Write a Java program that reads a series of input lines from given file “name.txt”, and sorts them into alphabetical order, ignoring the case of words. The program should use the merge sort algorithm so that it efficiently sorts a large file. Contents of names.text Slater, KendallLavery, RyanChandler, Arabella "Babe"Chandler, StuartKane, EricaChandler, Adam JrSlater, ZachMontgomery, JacksonChandler, KrystalMartin, JamesMontgomery, BiancaCortlandt, PalmerDevane, AidanMadden, JoshHayward, DavidLavery,k JonathanSmythe, GreenleeCortlandt, OpalMcDermott, AnnieHenry, DiGrey, MariaEnglish, BrookeKeefer, JuliaMartin, JosephMontgomery, LilyDillon, AmandaColby, LizaStone, Mary FrancesChandler, ColbyFrye, DerekMontgomery,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT