2015-05-09 23 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