Question
Write a class called ParseAndRewrap that reads in a file and wraps the words at a given line length. Do the following in the main
Write a class called ParseAndRewrap that reads in a file and wraps the words at a given line length.
Do the following in the main method of your class.
Prompt the user for the name of a plain text file and the maximum number of characters that can appear in output lines.
Read in all tokens from the file (remember, the Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace).
Use two different Scanner objects to read in the file word-by-word as shown in the pre-lab examples above. (It is possible to use one Scanner to do this, but using two scanners will give you practice for more complex parsing scenarios that you will need in the next project, so you must do it this way in this lab).
The first scanner uses hasNextLine() and nextLine() to read the file line-by-line.
The second scanner uses hasNext() and next() to read the line token-by-token.
Create the new output String by concatenating tokens until adding the next token would result in a line longer than the max length. Once the line is "full", it should be printed and a fresh output line started.
Since whitespace is stripped out of tokens by default, you will need to re-add a space after each word as you build output Strings ''if you aren't already at the maximum length of the line''. That is, if the next word to be added brings the line to the maximum length, then you will not add a space after it.
Keep track of the longest and shortest line in your reformatted output and report this information at the end.
Write a class called ParseAndRewrap that reads in a file and wraps the words at a given line length. Do the following in the main method of your class Prompt the user for the name of a plain text file and the maximum number of characters that can appear in output lines Read in all tokens from the file (remember, the Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace) Use two different Scanner objects to read in the file word-by-word as shown in the pre-lab examples above. (It is possible to use one Scanner to do this, but using two scanners will give you practice for more complex parsing scenarios that you will need in the next project, so you must do it this way in this lab) The first scanner uses hasNextLine(0 and nextLine0 to read the file line-by-line The second scanner uses hasNext0 and next0 to read the line token-by-token Create the new output String by concatenating tokens until adding the next token would result in a line longer than the max length. Once the line is "full, it should be printed and a fresh output line started Since whitespace is stripped out of tokens by default, you will need to re-add a space after each word as you build output Strings "if you aren't already at the maximum length of the line". That is, if the next word to be added brings the line to the maximum length, then you will not add a space after it Keep track of the longest and shortest line in your reformatted output and report this information at theStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started