如何在java中打印拆分字符串中的值的名称?

如何在java中打印拆分字符串中的值的名称?,java,split,Java,Split,我应该写一个程序,询问人们的姓名和年龄,然后打印出最年长的人的名字。(很抱歉,节目的某些部分不是英文的,但我希望有人能理解并提供帮助)。这是一项作业,我必须通过拆分字符串来完成 打印内容应如下所示: 詹姆斯,2岁 玛丽,2岁 杰西卡,1岁 詹妮弗,5岁 加布里埃尔,10岁 最年长的人的名字是加布里埃尔 我知道如何打印最高年龄,但不知道它的名字。我就是这样做的: public static void main(String[] args) { Scanner lukija = new Sc

我应该写一个程序,询问人们的姓名和年龄,然后打印出最年长的人的名字。(很抱歉,节目的某些部分不是英文的,但我希望有人能理解并提供帮助)。这是一项作业,我必须通过拆分字符串来完成

打印内容应如下所示:

詹姆斯,2岁 玛丽,2岁 杰西卡,1岁 詹妮弗,5岁 加布里埃尔,10岁

最年长的人的名字是加布里埃尔

我知道如何打印最高年龄,但不知道它的名字。我就是这样做的:

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);

假设我们有以下输入:

James,2
Mary,2
Jessica,1
Jennifer,5
Gabriel,10
我们可以逐行阅读,然后通过下面的课程找到最年长的人:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.*;

public class OldestPerson {
    public static class Person {
        private String name;
        private int age;

        Person(String name, int age) {
            this.name = name;
            this.age = age;
        }

        public int getAge() {
            return this.age;
        }

        public String getName() {
            return this.name;
        }
    }

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String line;
        PriorityQueue<Person> queue = new PriorityQueue<Person>((a, b) -> b.getAge() - a.getAge());
        while ((line = reader.readLine()) != null) {
            String[] tmp = line.split(",");
            queue.add(new Person(tmp[0], Integer.parseInt(tmp[1])));
        }
        if (!queue.isEmpty()) {
            Person oldestPerson = queue.poll();
            System.out.println(oldestPerson.getName());
        }
    }
}
导入java.io.BufferedReader;
导入java.io.InputStreamReader;
导入java.io.IOException;
导入java.util.*;
公共类老年人{
公共静态类人员{
私有字符串名称;
私人互联网;
Person(字符串名称,整数年龄){
this.name=名称;
这个。年龄=年龄;
}
公共整数getAge(){
返回这个年龄;
}
公共字符串getName(){
返回此.name;
}
}
公共静态void main(字符串[]args)引发IOException{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
弦线;
PriorityQueue=newpriorityqueue((a,b)->b.getAge()-a.getAge());
而((line=reader.readLine())!=null){
字符串[]tmp=line.split(“,”);
add(newperson(tmp[0],Integer.parseInt(tmp[1]));
}
如果(!queue.isEmpty()){
Person oldestPerson=queue.poll();
System.out.println(oldestPerson.getName());
}
}
}
另一种方法是直接比较人的年龄:

public static void main(String[] args) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    String line;
    String oldestPerson = "";
    int age = Integer.MIN_VALUE;
    while ((line = reader.readLine()) != null) {
        String[] tmp = line.split(",");
        if (age < Integer.parseInt(tmp[1])) {
            age = Integer.parseInt(tmp[1]);
            oldestPerson = tmp[0];
        }
    }
    System.out.println(oldestPerson);
}
publicstaticvoidmain(字符串[]args)引发IOException{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(System.in));
弦线;
字符串oldestPerson=“”;
int age=Integer.MIN_值;
而((line=reader.readLine())!=null){
字符串[]tmp=line.split(“,”);
if(年龄
这不是JavaScript。