将文件读入数组 - Java

时间:2015-05-09 12:20:56

标签: java arrays file initialization java.util.scanner

我正在练习java,并在线观看练习:

然而,我陷入了我需要的地步

Read the file again, and initialise the elements of the array

任务

  
      
  • 将表示成员列表的成员编写为数组
  •   
  • 构造函数应该使用String参数(文件名)
  •   
  • 使用扫描仪读取行并创建足以容纳文件的数组
  •   
  • 再次读取文件并初始化数组元素
  •   

当前代码

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

class Members {

    MemberElement[] members;

    public Members(String fileName) throws IOException {
        File myFile = new File(fileName);
        Scanner scan = new Scanner(myFile);

        int numOfLines = 0;
        while(scan.hasNextLine()) {
            scan.nextLine();
            numOfLines++;
        }
        scan.close();
        scan = new Scanner(myFile);

        members = new MemberElement[numOfLines];   
}

MemberElement Class

class MemberElement {

    private String name;
    private int number;
    private int birthDate;

    public MemberElement(String name, int number, int birthDate) {
        this.name = name;
        this.number = number;
        this.birthDate = birthDate;
    }

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

    public int getNumber() {
        return this.number;
    }

    public int getBirth() {
        return this.birthDate;
    }

    public String toString() {
        return getName() + " " + getNumber() + " " + getBirth(); 
    }
}

文本文件的内容:

Wendy Miller 7654 17-2-1960
Dolly Sheep 4129 15-5-1954
Dolly Sheep 5132 21-12-1981
Irma Retired Programmer 345 15-11-1946

4 个答案:

答案 0 :(得分:4)

它与计算线基本相同:

int numOfLines = 0;
while(scan.hasNextLine()) {
    scan.nextLine();
    numOfLines++;
}

但是,我们现在需要实际访问下一行。快速浏览Scanner docs告诉我,nextLine完全返回我们想要的内容。

int numOfLine = 0;
while(scan.hasNextLine()) {
    String line = scan.nextLine();
    members[numOfLine] = new MemberElement(line, numOfLine, /* birthDate */);
    numOfLine++;
}

答案 1 :(得分:2)

它表示数组的初始化元素。那就是

int index = 0;
while (scan.hasNextLine()) {
    // I assume MemberElement c-tor uses the read data somehow
    // otherwise what's the point in reading the file
    members[index++] = new MemberElement(scan.nextLine());
}

scan.close();

虽然任务本身似乎有些奇怪。

答案 2 :(得分:1)

试试这个:

class Members {

    MemberElement[] members;

    public Members(String fileName) throws IOException {
        File myFile = new File(fileName);
        Scanner scan = new Scanner(myFile);

        int numOfLines = 0;
        while (scan.hasNextLine()) {
            scan.nextLine();
            numOfLines++;
        }
        System.out.println("Lines-->"+numOfLines);
        scan.close();

        members = new MemberElement[numOfLines];
        scan = new Scanner(myFile);
        numOfLines = 0;
        while (scan.hasNextLine()) {
            String data[]=scan.nextLine().split(",");
            members[numOfLines]=new MemberElement(data[0], data[1], data[2]);
            System.out.println(members[numOfLines]);
            numOfLines++;
        }
    }
}
public class Test2{
    public static void main(String[] args) throws IOException {

     new Members("e:/temp.txt");
    }
}

MemberElement.java

class MemberElement {

    private String name;
    private String number;
    private String birthDate;

    public MemberElement(String name, String number, String birthDate) {
        this.name = name;
        this.number = number;
        this.birthDate = birthDate;
    }

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

    public String getNumber() {
        return this.number;
    }

    public String getBirth() {
        return this.birthDate;
    }

    public String toString() {
        return getName() + " " + getNumber() + " " + getBirth(); 
    }
}

输出:

Lines-->4
Wendy Miller  7654  17-2-1960
Dolly Sheep  4129  15-5-1954
Dolly Sheep  5132  21-12-1981
Irma Retired Programmer  345  15-11-1946

答案 3 :(得分:0)

如果需要,请使用列表并将其转换回数组:

public Members(String fileName) throws IOException {
    File myFile = new File(fileName);
    Scanner scan = new Scanner(myFile);
    List<String> list =new ArrayList<String>();

    while(scan.hasNextLine()) {
        list.add(scan.nextLine());
    }
    scan.close();
    scan = new Scanner(myFile);

    members = list.toArray();