printWinner
Category: Token-Based File Processing
Author: Benson Limketkai
Book Chapter: 5.4
Problem: printWinner
Write a static method printWinner that accepts a Scanner holding a sequence of names and numbers. Following each name is a series of one or more numbers. A person's sum is the total of the numbers until the next name. The method will print out the name of the person who has the highest sum less than or equal to 21. If everyone's sum is over 21, then the method will print "Everyone busted!" You may assume that there is at least one name. For example, given the following Scanners: Scanner input1 = new Scanner("alpha 10 5 9 bravo 8 charlie 11 9"); Scanner input2 = new Scanner("delta 10 3 9 echo 3 1 10 10"); Scanner input3 = new Scanner("foxtrot 11 5 6"); Scanner input4 = new Scanner("golf 4 8 hotel 10 6 india 9 8 7"); Calling printWinner will result in the following output: Call Output printWinner(input1) charlie is the winner! printWinner(input2) Everyone busted! printWinner(input3) Everyone busted! printWinner(input4) hotel is the winner! In the first example, the respective sums for alpha, bravo, and charlie are 24 (10 + 5 + 9), 8 (8), and 20 (11 + 9). The highest sum that is less than or equal to 21 belongs to charlie. In the second example, the sums are 22 (10 + 3 + 9) and 24 (3 + 1 + 10 + 10). Since no sum is less than or equal to 21, everyone busted.