3

I am practicing java, and looking at exercises online:

However, I am stuck at the point in which I need to

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

Task

  • Write class Members representing a list of members as an array
  • Constructor should take String argument (file name)
  • Use scanner to read lines and create array big enough to hold the file
  • Read the file again and initialise elements of the array

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

Contents Of Text File:

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
10
  • They ask you to read a file twice? Hmm Commented May 9, 2015 at 12:23
  • Yes, First time to read the number of lines and then close the scanner. Then Open the scanner again to initalise the array Commented May 9, 2015 at 12:24
  • Okay, where's your concrete problem? Opening the file a second time, or is it reading the lines into the array? Commented May 9, 2015 at 12:25
  • 1
    @RandomMath yes, I understand and is easy to implement as fxnn suggested, but I suppose they teach you a bad design (because you may perform full initialization reading the file once). Commented May 9, 2015 at 12:28
  • 1
    how is the birthdate in text file is converted to a int..? Commented May 9, 2015 at 12:50

4 Answers 4

4

It's basically the same like counting lines:

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

However, we now need to actually access that next line. A quick look into the Scanner docs tells me, that nextLine returns exactly what we want.

int numOfLine = 0;
while(scan.hasNextLine()) {
    String line = scan.nextLine();
    members[numOfLine] = new MemberElement(line, numOfLine, /* birthDate */);
    numOfLine++;
}
Sign up to request clarification or add additional context in comments.

4 Comments

I have just added the members class above
Okay, I added the numOfLine argument. Guess parsing the birthDate is the next step of your task :)
@fxnn , numofLine isn't the right thing to pass. The OP has added the contents of the file in the question.
Huh, I feel like this goes beyond the scope of the OP's original question to read a file into an array.
2

It says initialise elements of the array. So that would be

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

Although the task itself does seem to be somewhat strange.

1 Comment

I have just added the members class above
1

Try this:

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

3 Comments

Nice, but how to remove the extra space in front of name and number?
@CoolGuy just trim that extra space by calling trim() method
just do new MemberElement(data[0].trim(), data[1].trim(), data[2].trim())
0

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

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

1 Comment

For explaination: the ArrayList shown here implements a self-growing array, so in the end you don't have to count the lines in the beginning, but can rely on the array growing as you need it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.