Reading a file into an array - Java

I am doing java and watching exercises online:

However, I am stuck at the point where I need

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

      

Task

  • Writing class members representing a list of elements as an array
  • The constructor must take a String argument (filename)
  • Use a scanner to read the strings and create an array large enough to hold the file
  • Read the file again and initialize the array elements

Current code

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

      

Text file content:

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


source to share


4 answers


This is basically the same as counting lines:

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

      



However, now we need to actually access the next line. A quick look at the Documentation of the scanner tells me that it nextLine

returns exactly what we want.

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

      

+4


source


It specifies the initializing elements of the array. So it will be

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

scan.close();

      



Although the task itself seems somewhat strange.

+2


source


Try the following:

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

      

Output:

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

      

+1


source


Use a list instead and convert it back to an array if you like:

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


source







All Articles