Java 从文件中查找最低年龄并在数组中打印出相应的名称?

Java 从文件中查找最低年龄并在数组中打印出相应的名称?,java,Java,我需要一个程序的帮助,该程序从一个名为Stuff.txt的文件中读取名称和年龄。我可以打印两个数组,但我还需要找到最低年龄并打印出相应的名称。我的代码不起作用,我不知道为什么?不需要回路或开关。我的东西。到目前为止,txt包含以下输入: Sam 21 Ram 22 Hero 23 以下是我迄今为止所做的工作: import java.io.*; import java.util.*; public class nameAge { public static void main(St

我需要一个程序的帮助,该程序从一个名为Stuff.txt的文件中读取名称和年龄。我可以打印两个数组,但我还需要找到最低年龄并打印出相应的名称。我的代码不起作用,我不知道为什么?不需要回路或开关。我的东西。到目前为止,txt包含以下输入:

  Sam 21 Ram 22 Hero 23
以下是我迄今为止所做的工作:

 import java.io.*;
 import java.util.*;
 public class nameAge
 {
public static void main(String[] args) throws FileNotFoundException
{
  ArrayList<String> names = new ArrayList<String>();
  ArrayList<Integer> ages = new ArrayList<Integer>();

  Scanner inFile = new Scanner(new File("stuff.txt")); 
  int i = 0; 
  while (inFile.hasNextLine()) { 
  String line = inFile.nextLine();
  Scanner lineScanner = new Scanner(line); 
  while(lineScanner.hasNext()){
  names.add(lineScanner.next());
  ages.add(lineScanner.nextInt());
  }
   System.out.println(names);
   System.out.println(ages);
  }


   int idx = -1;  
   int d= 100000;  
   for( i = 0; i < 3; i++) 
     if(ages < d) 
   { 
     d = ages[i]; 
     idx = i; 
   } 
   System.out.println("Lowest age "+ ages[idx] + "Corresponding Name" + name[idx] ); 

  }
  }

循环浏览您的年龄列表,该索引就是您姓名中的索引,因为年龄和姓名之间的比率为1:1

按要求填写完整代码

public static void main(String[] args){
        ArrayList<String> names = new ArrayList<String>();
        ArrayList<Integer> ages = new ArrayList<Integer>();

        //YOUR LOADING CODE GOES HERE

        int index = getIndexOfMin(ages);
        System.out.println(names.get(index) + " is " + ages.get(index));

    }

    public static int getIndexOfMin(ArrayList<Integer> data) {
        float min = Float.MAX_VALUE;
        int index = -1;
        for (int i = 0; i < data.size(); i++) {
            Integer f = data.get(i);
            if (Float.compare(f.floatValue(), min) < 0) {
                min = f.floatValue();
                index = i;
            }
        }
        return index;
    }

对ExcelledProducts发布的代码进行轻微修改

import java.io.*;
import java.util.*;

public class NameAge {

public static void main(String[] args) throws FileNotFoundException
{
  ArrayList<String> names = new ArrayList<String>();
  ArrayList<Integer> ages = new ArrayList<Integer>();

  Scanner inFile = new Scanner(new File("stuff.txt")); 
  int i = 0; 
  while (inFile.hasNextLine()) { 
  String line = inFile.nextLine();
  Scanner lineScanner = new Scanner(line); 
  while(lineScanner.hasNext()){
  names.add(lineScanner.next());
  ages.add(lineScanner.nextInt());
  }
   System.out.println(names);
   System.out.println(ages);
  }

  int old_age = 0;
  int old_age_i = 0;
  int count = 0;
  for(int age : ages){
       if (old_age == 0)
            old_age = age;
       else
            if (old_age > age){
                 old_age = age;
                 old_age_i = count;
       count ++;}
            else continue;
  }
  String name = names.get(old_age_i);
  System.out.println(name);
  String theAge = ages.get(old_age_i);
  System.out.println(theAge);
  }
}

我是Java初学者。你介意让我更清楚一点吗?我不知道我应该把你给我的代码放在哪里。这个代码应该替换你的int-idx=-1代码;到System.out.println。。。我会用完整的代码更新我的答案非常感谢。请用完整的代码更新您的答案。您还可以帮我打印年龄,而不仅仅是姓名吗?旁注:请看一下如何正确缩进代码。不一定更好,但运行得更早,在检索正确的年龄时遇到问题。编辑后还没有尝试过她的/他的代码。我甚至连看都没看