Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using NetBean Skills Java I/O classes Catching predefined Java File Exceptions Reading text files with the Scanner class Writing text files with the PrintWriter class

Using NetBean

Skills

Java I/O classes

Catching predefined Java File Exceptions

Reading text files with the Scanner class

Writing text files with the PrintWriter class

Description

In this assignment you'll write a simple text formatting program. It will read words from an input file and write them to an output file, removing extra whitespace and filling each line with as many words as will fit up to a maximum column width (similar to a column of text in a newspaper). The maximum column width, input text file name, and output text file name are entered interactively when the program starts..

Your program needs to perform the following steps:

Prompt for the maximum formatted output width. The range is 30 to 100 characters.

Prompt for the file name of the input text file. Use a File object to test that the file exists, and can be read. If so, open the file for reading using the Scanner class. Use a do-while loop to repeat these steps until a valid input file is entered.

Prompt for the file name of the output file to write the formatted words to. Use a File object to test that the file doesn't already exist, and if it does, prompt to check for overwriting the file. Open the file for writing using a PrintWriter. Use a do-while loop to repeat these steps until a valid output file is entered.

Write a line of * characters the width of the formatted output as a heading in the output file (no formatted output line should be longer than this heading line) E.g. if 45 was entered as the maximum output width, this line should have 45 * characters.

Loop, reading words (text between whitespace) from the input file using Scanner.next(), appending them to a line of text being formatted, with one space character between words. Keep appending words until the current word would cause the line to become longer than the output column with. If so, write the line to the output file, reset it to contain just the new word, and continue reading. Continue reading until the end-of-file is encountered, using Scanner.hasNext().

Do not read the entire file into memory, and do not use an array or a large string to hold multiple lines in memory at once.

At the end of input, close the output PrintWriter to flush any buffered output to the file.

Sample Output

Given the following input file:

The quick brown fox jumped over the big lazy brown dog while quickly running from the big bad wolf across the expansive rolling green meadow towards the crumbling stone wall near the waterfall.

Specifying a formatted output width of 30 should produce the following output:

Formatted output text follows...

******************************

The quick brown fox jumped over the big lazy brown dog while quickly running from the big bad wolf across the expansive rolling green meadow towards the crumbling stone wall near the waterfall.

Test Data

Test using the following downloaded data files. Run each file with output widths of 40 and 70. When submitting this homework, include all of the formatted output files and your source code.

Use the attached HW8_1.txt and HW8_2.txt text files as data for testing your formatting program.

NetBeans IDE

When using NetBeans for this homework, the .txt data files must be located one level above the /src directory containing the .java source files, i.e. in the same directory as the build.xml file.

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Great Management Truths... 1. Have a plan. Hip shooters die young. Build your plans, and execute them in one straight, continuous line. 2. Be a realist. Many plans might seem to lead to a happy never-never land of easy profit growth and tranquility for all. Don't allow yourself or others to smoke this pipe. 3. Start with customer needs. Your inventory will turn over a lot faster if it contains what people need. Better yet if its also something they want. 4. Use your common sense. It is simply astonishing how far a little common sense will go, and how seldom it is used. 5. Have an advantage. If you don't have one, build one. Otherwise, you will set dead in the water. 6. Recognize your limitations. Enthusiasm for your product and your company is important, but you will be marching steadily into a swamp if you assume that everything you do is better than anything your competition does. If you competitor has a lock on a particular piece of the market, recognize your limitations. 7. Establish objectives. If you have nowhere to go, you will certainly get there. 8. Set your strategies. You cannot be all things to all customers. Fish where the fishing is best, and you will eat better than those who try to fish everywhere at once. 9. Execute well. There are usually so many slips twixt cup and lip that an average plan, well executed, will beat out a better, but badly executed, plan every time. 10. Analyze. If you don't analyze, you can't learn. And if you don't learn, you won't grow.

------------------------------------------------------------------------------------------------------------------------

"Ode to 'Real' Programmers" Real programmers don't do documentation. Users should consider themselves lucky to get any programs at all, and take what they get. Real programmers don't comment their code. If it was hard to write, it should be hard to understand. Real programmers don't need comments - the code is obvious. Real programmers don't eat quiche. They eat twinkies, M & M's, Snickers, Ring-Dings, and other healthful food. Real programmers don't play tennis, or any other sport which requires you to change clothes. Mountain climbing is OK, and real programmers wear their hiking boots to work in case a mountain should suddenly spring up in the middle of the room. Real programmers don't wear neckties Real programmer's programs never work the first time. But if you throw them on the computer, they can be patched in only a few 30 hour debugging sessions. Real programmers never work 9 to 5. If they are around at 9 A.M., its because they were up all night. Real programmers don't write in Pascal, ADA, Bliss, or any of those silly 'strongly typed' languages. Strong typing is for people with weak memories. Real programmers arrive at work in time for lunch.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Entity Alignment Concepts Recent Advances And Novel Approaches

Authors: Xiang Zhao ,Weixin Zeng ,Jiuyang Tang

1st Edition

9819942527, 978-9819942527

More Books

Students also viewed these Databases questions