Question: Requirements Create a new Eclipse project and place the files KansasTownsPop1.txt and KansasTownsPop2.txt into the root folder. Olathe 133062 Lenexa 51042 Emporia 24560 Wichita 382368

Requirements

Create a new Eclipse project and place the files KansasTownsPop1.txt and KansasTownsPop2.txt into the root folder.

Olathe 133062

Lenexa 51042

Emporia 24560

Wichita 382368

Leawood 34395

Eudora 6303

Gardner 20667

Shawnee 64599

Topeka 127215

Freeport 5

Write code that adheres to the following outline:

Open the KansasTownsPop1.txt file. If the file does not exist, display an error message that states KansasTownsPop1.txt does not exist. Then exit the program with a status of 1. You may not hardcode the file name into the error message; rather use the getName() method from the File object.

Use a loop to read in each line. Since the city names do not have spaces you can use next() and nextInt() inside the loop to read the city name and population. Since we havent yet used arrays, build a single string of city names by continuously appending the name of the city just read and a space (Hint: Declare a variable named cityNames and initialize it to the empty string before the loop). Also sum each population read to a running total.

After the loop, use a statement of the following form to display the results:

System.out.printf("The total population of %sis %d.",

cityNames, totalPopulation);

Test your code by only changing the line that opens KansasTownsPop1.txt to KansasTownsPop2.txt. The program should still run correctly with new results.

Sample runs

KansasTownsPop1.txt (with file error)

KansasTownsPop1.txt does not exist.

KansasTownsPop1.txt (no error)

The total population of Olathe Lenexa Emporia Wichita is 591032.

KansasTownsPop2.txt (with file error)

KansasTownsPop2.txt does not exist.

KansasTownsPop2.txt (no error)

The total population of Leawood Eudora Gardner Shawnee Topeka Freeport is 253184.

Submission

Check off the program in class.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!