1 import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.util.Scanner;
4
5 /**
6 This program displays the most common baby names. Half of boys and girls in
7 the United States were given these names in the 1990s.
8 */
9 public class BabyNames
10 {
11 public static final int LIMIT = 50;
12
13 public static void main(String[] args) throws FileNotFoundException
14 {
15 Scanner in = new Scanner(new File("babynames.txt"));
16
17 double boyTotal = 0;
18 double girlTotal = 0;
19
20 while (boyTotal < LIMIT || girlTotal < LIMIT)
21 {
22 int rank = in.nextInt();
23 System.out.print(rank + " ");
24
25 boyTotal = processName(in, boyTotal);
26 girlTotal = processName(in, girlTotal);
27
28 System.out.println();
29 }
30
31 in.close();
32 }
33
34 /**
35 Reads name information, prints the name if total >= 0, and adjusts the total.
36 @param in the input stream
37 @param total the total percentage that should still be processed
38 @return the adjusted total
39 */
40 public static double processName(Scanner in, double total)
41 {
42 String name = in.next();
43 int count = in.nextInt();
44 double percent = in.nextDouble();
45
46 if (total < LIMIT) { System.out.print(name + " "); }
47 total = total + percent;
48 return total;
49 }
50 }