Created
December 10, 2023 04:36
-
-
Save mreed4/8399f8871a5f6fcbfc15e65b7ad5ba2f to your computer and use it in GitHub Desktop.
MOOC.fi - PersonalDetails - Solution 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
public class PersonalDetails { | |
public static void main(String[] args) { | |
Scanner scanner = new Scanner(System.in); | |
int sum = 0; | |
String longestName = ""; | |
int count = 0; | |
while (true) { | |
String input = scanner.nextLine(); | |
if (input.equals("")) { | |
break; | |
} | |
String[] person = input.split(","); | |
String name = person[0]; | |
int birthYear = Integer.valueOf(person[1]); | |
sum += birthYear; | |
count++; | |
if (name.length() > longestName.length()) { | |
longestName = name; | |
} | |
} | |
double average = 1.0 * sum / count; | |
System.out.println("Longest name: " + longestName); | |
System.out.println("Average of the birth years: " + average); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment