Java的新手,我有一个问题

给我一个文本文件,要求我找到收入最高的员工,并打印出他们的信息(fName,Lname,ID)

文本文件是这样的:

Date of birth fName lName    wage hr   work emp ID
12/03/1929    Detzk Fyshe    37   49   07036310484
04/17/1930    Cauus Walden   38   52   63612537553
07/12/1930    Barth Harling  43   72   42101524036
07/16/1930    Bartl Barnhill 43   62   48621748867


我设法找到最高工资。但是不知道如何打印信息

到目前为止,这里是我的代码:

public static void main(String[] args) {

    File inFile =  new File ("dataSet.txt");
    ArrayList <String> inData = new ArrayList <String>();


    String strline;


   try
   {
       FileInputStream fstream = new FileInputStream(inFile);
       BufferedReader br = new BufferedReader (new InputStreamReader (fstream));
       while ((strline = br.readLine()) != null)
       {
                    strline = strline.trim();
                    if ((strline.length()!=0)) inData.add(strline);
                    }


   } catch (Exception e)    {
                            System.err.println("Error CANNOT FIND FILE!");
                            }


       // Max wage finder *start*

    int maxWage=0;
    for (int i=0; i<inData.size(); i++){

    String [] word = inData.get(i).split(" ");
     int wage=Integer.parseInt (word[3]);
     int hrWork=Integer.parseInt (word[4]);
     int earn = wage*hrWork;

      if (earn>maxWage){
          maxWage=earn;
      }
}
  System.out.println("Max Wage in $:"+maxWage);

//max wage finder *end*

最佳答案

您可以添加一个int maxWageOwner
可以跟踪列表中最后一个maxWage来自哪个项目。

因此,在if (earn > maxWage) {...}内只需添加maxWageOwner = i;
并在for循环之前初始化maxWageOwner

然后,要打印信息,只需访问inData.get(maxWageOwner)

09-26 11:33