Java ArrayList未返回预期数据

Java ArrayList未返回预期数据,java,arraylist,Java,Arraylist,我不知道我在做什么,如果能得到任何帮助,我将不胜感激。 我正在读取具有以下代码的文本文件: 7 10 416-555-6667 Burgess Smith 15 15 905-777-8888 Thomas Patel 10 20 905-111-2222 Morris J. Stevenson 5 25 416-222-3333 Spencer Larson 30 30 416-333-4444 Adams Doe 18 35 905-122-5454 Price Hanks 15 40 90

我不知道我在做什么,如果能得到任何帮助,我将不胜感激。 我正在读取具有以下代码的文本文件:

7
10 416-555-6667 Burgess Smith 15
15 905-777-8888 Thomas Patel 10
20 905-111-2222 Morris J. Stevenson 5
25 416-222-3333 Spencer Larson 30
30 416-333-4444 Adams Doe 18
35 905-122-5454 Price Hanks 15
40 905-343-5151 Clement L. Webster 8
private static void fileReader() throws FileNotFoundException
{
    int eId = 0;
    String nme = "";
    String phne = "";
    int yrs = 0;
    String line ="";
    Employee emp = new Employee(eId, nme, phne, yrs);
    File inputfile = new File("Emp.txt");
    Scanner in = new Scanner(inputfile);
    n = in.nextInt() - 1;
    in.nextLine();
    in.useDelimiter("");
    for (int i=0;i<=n;i++)          
    {
        int l = 0;
        int m = 0;
        int n = 0;
        line = in.nextLine();
        while (Character.isDigit(line.charAt(l)))
        {
            l++;
        }
        m = l + 1;
        while (!Character.isLetter(line.charAt(m)) && !Character.isWhitespace(line.charAt(m)))
        {
            m++;
        }
            n = m + 1;
            while (!Character.isDigit(line.charAt(n)))
            {
                n++;
            }
            eId = Integer.parseInt(line.substring(0, l));
            emp.setEmpId(eId);
            phne = line.substring(l + 1, m - 1);
            emp.setTelephone(phne);
            nme = line.substring(m + 1, n - 1);
            emp.setName(nme);
            yrs = Integer.parseInt(line.substring(n));
            emp.setYears(yrs);
            empArr.add(i, emp);
        }
    in.close();
}
}

当我在其for循环之外调用ArrayList的get方法时,每个索引处的文本将被最后一个索引处的文本覆盖

我想我在这里遗漏了一些构造函数和对象的基本概念


感谢您的帮助。谢谢。

你的直觉是正确的,你错过了emp对象的创建。必须将emp对象创建移动到循环中

  • 您的Employee类和getter/setter方法编写正确
  • 重写fileReader()方法,如下所示:-

    String line ="";    
    //Declare an Arraylist for an Employee
    List<Employee> employee = new ArrayList<Employee>();            
    //Read a file
    File inputfile = new File("Emp.txt file path");    
    Scanner in = new Scanner(inputfile);
    
    //Reading a number from a first sentence 
    int n = Integer.parseInt(in.nextLine());     
    
    for (int i=0;i<n;i++) {
        // Reading each sentence
        line = in.nextLine();   
        //Parse an Emp id 
        int eId = Integer.parseInt(line.substring(0, 2));
        //Parse a phone number  
        String phone = line.substring(3, 14);
        //Parse a name         
        String name = line.split("\\d+")[4];          
        //Parse years
        int years = Integer.parseInt(line.split("\\D+")[4]); 
        //Now create an object by putting all above values in a constructor
        Employee emp1 = new Employee(eId, name, phone, years);
        //Add that object in an arraylist
        employee.add(emp1);
    }
    
    //As you have overridden toString method, print an arraylist
    System.out.println(emp.toString());   
    //Closing the scanner
    in.close();
    }
    
    字符串行=”;
    //为员工声明Arraylist
    List employee=new ArrayList();
    //读文件
    File inputfile=新文件(“Emp.txt文件路径”);
    扫描仪输入=新扫描仪(输入文件);
    //读第一句话中的数字
    int n=Integer.parseInt(in.nextLine());
    
    对于(int i=0;i在什么ArrayList上获取方法?您的直觉是正确的,您缺少emp对象创建。您必须将emp对象创建移动到循环中。以下是我尝试使用的示例:@Gangadhar Kairi我将尝试此方法thanks@GangadharKairi成功了,谢谢
    String line ="";    
    //Declare an Arraylist for an Employee
    List<Employee> employee = new ArrayList<Employee>();            
    //Read a file
    File inputfile = new File("Emp.txt file path");    
    Scanner in = new Scanner(inputfile);
    
    //Reading a number from a first sentence 
    int n = Integer.parseInt(in.nextLine());     
    
    for (int i=0;i<n;i++) {
        // Reading each sentence
        line = in.nextLine();   
        //Parse an Emp id 
        int eId = Integer.parseInt(line.substring(0, 2));
        //Parse a phone number  
        String phone = line.substring(3, 14);
        //Parse a name         
        String name = line.split("\\d+")[4];          
        //Parse years
        int years = Integer.parseInt(line.split("\\D+")[4]); 
        //Now create an object by putting all above values in a constructor
        Employee emp1 = new Employee(eId, name, phone, years);
        //Add that object in an arraylist
        employee.add(emp1);
    }
    
    //As you have overridden toString method, print an arraylist
    System.out.println(emp.toString());   
    //Closing the scanner
    in.close();
    }