Untitled
unknown
java
3 years ago
2.0 kB
81
Indexable
import java.util.ArrayList;
import java.util.Scanner;
public class PersonalDetails {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
boolean firstAssigned = false; //This variable is to know whether 1st iteration has assigned some value or not
int birthYear = 0; String name = ""; //These variable stores birth Year and name from input
int lengthOfName = 0; //This variable stores the length of name
int sum = 0; //This variable stores the sum of birth years
int lenthOfLongestName = 0; //This variable stores the length of Longest Name
String longestName = ""; //This variable stores the longest name
int i = 0; //This variable stores the numbers of inputs untill input is empty
double average = 0.0; //This variable stores the average of all birth years
while(true){
//This loop takes input from user and then prints longest name
//and average of all the birth years
String str = scanner.nextLine();
if(str.isEmpty()){
//This block of code breaks loop when input is empty
break;
}
String[] words = str.split(",");
birthYear = Integer.valueOf(words[1]);
name = words[0];
lengthOfName = name.length();
if(firstAssigned == false){
lenthOfLongestName = lengthOfName;
longestName = name;
firstAssigned = true;
}
if(lengthOfName > lenthOfLongestName){
lenthOfLongestName = lengthOfName;
longestName = name;
}
sum = sum + birthYear;
i++;
}
average = 1.0 * sum/i;
System.out.print("Longest name: " + longestName + "\n"
+ "Average of birth years: " + average);
}
}
Editor is loading...