Write a java program to read the names and ages of 10 people and print the name of the oldest person

1 answer

Here's a Java program that reads the names and ages of 10 people and prints the name of the oldest person:

```java
import java.util.Scanner;

public class OldestPerson {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String[] names = new String[10];
int[] ages = new int[10];

for (int i = 0; i < 10; i++) {
System.out.println("Enter the name of person " + (i + 1) + ": ");
names[i] = scanner.nextLine();

System.out.println("Enter the age of person " + (i + 1) + ": ");
ages[i] = scanner.nextInt();
scanner.nextLine(); // Clear newline symbol left by nextInt
}

int oldestIndex = 0;
for (int i = 1; i < ages.length; i++) {
if (ages[i] > ages[oldestIndex]) {
oldestIndex = i;
}
}

System.out.println("The oldest person is: " + names[oldestIndex] + " with age of " + ages[oldestIndex] + " years.");
}
}
```

To run the program, save it in a file called `OldestPerson.java`, then compile and run it using these commands in the terminal:

```bash
javac OldestPerson.java
java OldestPerson
```

The program will prompt you to enter the names and ages of 10 people, and then print the name of the oldest person.