2015-05-09 73 views
3

我在练Java和寻找练习在线:阅读文件到数组 - Java的

不过,我停留在点上,我需要

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

任务

  • 写作班成员列表成员列表
  • 构造应采取字符串参数(文件名)
  • 用扫描仪读取线和创建数组大到足以容纳的文件
  • 再次读取该文件,并初始化数组

当前的元素代码

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 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 
+0

他们问你读取文件两次吗?嗯 –

+0

是的,首次读取行数,然后关闭扫描仪。然后再次打开扫描仪以启动阵列 – RandomMath

+0

好的,您的具体问题在哪里?第二次打开文件,还是读取数组中的行? – fxnn

回答

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(); 
+0

为了说明:此处显示的ArrayList实现了一个自增长数组,因此最后不必在开始时计算行数,但可以根据需要随时增大数组。 – fxnn

+0

@fxnn yep就是这样 –

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

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

+0

我刚才在 – RandomMath

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++; 
} 
+0

之上加了成员类我刚才加了 – RandomMath

+0

以上的成员类好吧,我加了'numOfLine'这个参数。猜解析'birthDate'是你的任务的下一步:) – fxnn

+0

@fxnn,'numofLine'不是正确的事情通过。 OP已经在问题中添加了文件的内容。 –

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 
+0

不错,但如何删除名称和数字前面的额外空间? –

+0

@CoolGuy只是通过调用'trim()'方法修改这个额外的空间 – Rustam

+0

只需要'new MemberElement(data [0] .trim(),data [1] .trim(),data [2] .trim()) ' – Rustam