user3459720
user3459720

Reputation: 29

Object needs to created from the file and placed into an array

I am trying to create an object for each line of text and as each object is created, place it into an array. I'm struggling to place it into an array. This is my code:

    File inFile = new File("shareholders.txt");
    Scanner inputFile = new Scanner(inFile);
    String str;
    Shareholder shareholder = new Shareholder();

    while (inputFile.hasNext()) {

        str = inputFile.nextLine();
        String tokens[] = str.split(",");
        shareholder.setID(tokens[0]);
        shareholder.setName(tokens[1]);
        shareholder.setAddress(tokens[2]);
        shareholder.setPortfolioID(tokens[3]);

    }

Upvotes: 0

Views: 69

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

I think a list of shareholder objects might make the most sense here:

File inFile = new File("shareholders.txt");
Scanner inputFile = new Scanner(inFile);
String str;
List<Shareholder> list = new ArrayList<>();

while (inputFile.hasNext()) {
    Shareholder shareholder = new Shareholder();
    str = inputFile.nextLine();
    String tokens[] = str.split(",");
    shareholder.setID(tokens[0]);
    shareholder.setName(tokens[1]);
    shareholder.setAddress(tokens[2]);
    shareholder.setPortfolioID(tokens[3]);

    list.add(shareholder);
}

The reason a list makes sense here is because you might not know how many shareholders are present in the input file. Hence, an array might not work so well in this case (and even if the number of shareholders were fixed it could change at some later date).

Upvotes: 1

Anshuman Mitra
Anshuman Mitra

Reputation: 130

If you have a fixed number of shareholders, you can do this -

  File inFile = new File("shareholders.txt");
    Scanner inputFile = new Scanner(inFile);
    String str;
    int i=0;
    Shareholder[] shareholder = new Shareholder[n];

    while (inputFile.hasNext()) {
        str = inputFile.nextLine();
        String tokens[] = str.split(",");
        shareholder[i++] = new Shareholder(tokens[0],tokens[1],tokens[2],tokens[3]);
    }

Or if dont know the number of shareholders, then you can use list -

File inFile = new File("shareholders.txt");
Scanner inputFile = new Scanner(inFile);
String str;
List<Shareholder> list = new ArrayList<>();

while (inputFile.hasNext()) {
    Shareholder shareholder = new Shareholder();
    str = inputFile.nextLine();
    String tokens[] = str.split(",");
    list.add(new Shareholder(tokens[0],tokens[1],tokens[2],tokens[3]));
}

Upvotes: 1

Korashen
Korashen

Reputation: 2191

Before reading the file, you can not know how many lines the file has. The information about the number of lines is important to initialize your array with that specific size or otherwise you would need to extend your array multiple times by creating a new, bigger one. Which is bad practice and bad performance.

But instead of working with an array itself, use an arraylist for easier usage and just return a simple array, which can be received from the arraylist you worked with.

My suggestion as a solution for this issue is the following. Please note that the following code is not 100% complete and will not run in it's state. It is your job to complete it and make it run.

public void readFileIntoArray(String filename, Shareholder[] targetArray)
{
    File sourceFile = new File(filename);
    // Read in the file to determine the number of lines (int numberOfLines)
    ArrayList<Shareholder> lines = new ArrayList<>(numberOfLines);

    Shareholder sh;
    while(file.hasNext())
    {
        sh = new Shareholder();
        //Parse data into Shareholderobject
        lines.add(sh);
    }
    return lines.toArray();
}

Upvotes: 0

Related Questions