1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.util.Scanner; public class PersonalDetails { public static void main(String[] args) { int nameLength = 0; String longestName = ""; int sumOfBirthYears = 0; int dataCount = 0; Scanner scanner = new Scanner(System.in); while (true) { String input = scanner.nextLine(); if (input.equals("")) { break; } else { dataCount = dataCount + 1; String[] data = input.split(","); sumOfBirthYears = sumOfBirthYears + Integer.valueOf(data[1]); if (data[0].length() > nameLength) { nameLength = data[0].length(); longestName = data[0]; } } } System.out.println("Longest name: " + longestName); System.out.println("Average of the birth years: " + ((double) sumOfBirthYears / dataCount)); } } |
Direct link: https://paste.plurk.com/show/N01Trc5ZqJv0XXhirvmJ