Skip to content

Instantly share code, notes, and snippets.

@mreed4
Created December 10, 2023 04:36
Show Gist options
  • Save mreed4/8399f8871a5f6fcbfc15e65b7ad5ba2f to your computer and use it in GitHub Desktop.
Save mreed4/8399f8871a5f6fcbfc15e65b7ad5ba2f to your computer and use it in GitHub Desktop.
MOOC.fi - PersonalDetails - Solution 2
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