Names.java

advertisement
Names.java
Objective: To use String methods and file I/O to solve a problem.
Background:
Every year, millions of babies are born in the U.S.A. One of the most important decisions is the baby’s name.
Some names are more popular than others at different times. In 1900, John and Mary were the most popular boy
and girl names. In 2010, Jacob and Emily were the most popular.
You will be given a file of names and their popularity index for the years 1900 to 2010 in ten year increments.
Your task will be to search through the file and print out a chart of popularity for a selected name.
Assignment:
1) First, write a program to use the Scanner next() method to search the text file names.txt for a specific name.
When you find the given name in the file, you read the following 11 integers into an array. The result should
be printed to the screen on a single line, like below.
% java Names
Please enter the name you're searching for
Lisa
0
0
0
0
0
391
26
1
8
36
-> Lisa
139
478
In this example, Lisa was the 478th most popular name in 2010. In 1900, Lisa was not popular at all and
earned a 0.
2) Once you have accomplished the task above, you should work with the array to create a chart. The chart
should have column headings indicating the years 1900 through 2010, in increments of 10. You should also
include row headings indicating the popularity index, in increments of 20, from 20 to 1000. The popularity
value for each year should be printed in the appropriate row and column. Keep in mind that if the number is
not printed in the given row/column, then dashes should be printed. You should make use of printf. See the
sample run outputs below.
% java Names
Please enter the name you're searching for :
Harry
20
40
60
80
100
120
140
160
180
200
220
12
1900
12
---------------------
14
19
1910
14
---------------------
24
40
1920
19
---------------------
52
1930
--24
-------------------
92
148
1940
--40
-------------------
209
1950
----52
-----------------
-> Harry
300
446
1960
--------92
-------------
573
1970
--------------148
-------
1980
--------------------209
1990
-----------------------
2000
-----------------------
2010
-----------------------
240
260
280
300
320
340
360
380
400
420
440
460
480
500
520
540
560
580
600
620
640
660
680
700
720
740
760
780
800
820
840
860
880
900
920
940
960
980
1000
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
------300
-----------------------------------------------------------------------
----------------------446
-------------------------------------------------------
----------------------------------573
-------------------------------------------
1980
--------------------------------324
-----------
1990
------------121
-------------------------------
2000
------62
-------------------------------------
2010
----------111
---------------------------------
% java Names
Please enter the name you're searching for :
Marissa
20
40
60
80
100
120
140
160
180
200
220
240
260
280
300
320
340
360
380
400
420
440
0
1900
---------------------------------------------
0
0
1910
---------------------------------------------
0
0
1920
---------------------------------------------
0
0
807
1930
---------------------------------------------
324
1940
---------------------------------------------
121
1950
---------------------------------------------
-> Marissa
62
111
1960
---------------------------------------------
1970
---------------------------------------------
460
480
500
520
540
560
580
600
620
640
660
680
700
720
740
760
780
800
820
840
860
880
900
920
940
960
980
1000
---------------------------------------------------------
---------------------------------------------------------
---------------------------------------------------------
---------------------------------------------------------
---------------------------------------------------------
---------------------------------------------------------
---------------------------------------------------------
------------------------------------807
-------------------
% java Names
Please enter the name you're searching for
Shrikavya not found in database.
-> Shrikavya
---------------------------------------------------------
---------------------------------------------------------
---------------------------------------------------------
---------------------------------------------------------
Download