Garmaine Staff asked 2 years ago

I'm supposed to write a program that asks for people's name and age and then prints the oldest person's name. (Sorry some parts of the programming are not in english, but I hope someone understands enough to help). It's an assignment and I have to do it by splitting the string.

The print should be like this: (these should not show up on the same row, I don't know why it's doing this. Only the name and the age are on the same row)

James,2 Mary,2 Jessica,1 Jennifer,5 Gabriel,10

The name of the oldest: Gabriel

I know how to print the highest age, but not the name of it. This is how i've done it:

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

    int oldest = -1;
    while (true) {
        String mjono = lukija.nextLine();
        if (mjono.equals("")) {
            break;
        }
        String[] pieces = mjono.split(",");
        int age = Integer.valueOf(pieces[1]);
        if (age > oldest) {
            oldest = age;
        }
    }
    System.out.println("The oldest age: " + oldest);