Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

SQL Database Programming

Authors: Chris Fehily

1st Edition

1937842312, 978-1937842314

More Books

Students also viewed these Databases questions