0

I currently have my program reading the first object using BufferedReader but I am unsure how to read more then one object. Here is the code that I have reading from the file:

    public Stock getData(){

    StringTokenizer row;
    Stock aStock = new Stock();

    try{
        BufferedReader inbuffer = new BufferedReader(new FileReader(fileName));
        String inputString;
        inputString = inbuffer.readLine();
        if(inputString != null){
            row = new StringTokenizer(inputString, DELIMTER);
            if(row.countTokens() == 4){
                aStock.setStockName(row.nextToken());
                aStock.setStockQuantity(Integer.parseInt(row.nextToken()));
                aStock.setPurchasePrice(Double.parseDouble(row.nextToken()));
                aStock.setCurrentPrice(Double.parseDouble(row.nextToken()));

            }
        }
        inbuffer.close();
    }
    catch(IOException ioe){
        JOptionPane.showMessageDialog(null, ioe.getMessage(), "File Read Error", JOptionPane.ERROR);
    }
    return aStock;
}

The file I am reading from looks like this: enter image description here

and then the section of code that calls to my bufferedReader looks like this:

    public void loadFile(){
    StockIO stockRead = new StockIO();
    jFileChooser1.showOpenDialog(jPanel3);
    File file = jFileChooser1.getSelectedFile();
    stockRead.loadFileName(file.getName());
    stockArr.add(stockRead.getData());

    int index = 0;
    if(stockArr.get(index) != null){
        DLM.addElement(stockArr.get(0).getStockName());
        index ++;
    }


    listStock.setModel(DLM);

}

So I am trying to get it where my bufferedReader will read and send both lines of code through.Currently if I run this it will send through the "Shawn" line with all object information but I want the "test" line as well. Thank you for your time looking at this.

1 Answer 1

1

Your code is currently stopping after it reads the first line because there is no loop to continue iterating through your whole file.

You can use a while loop to check if there is a line to read while looping through your lines:

while ((line = inbuffer.readLine()) != null) {
   // Process each line
}

import java.util.ArrayList;

public ArrayList<Stock> getData(){

    StringTokenizer row;

    ArrayList<Stock> stockList = new ArrayList<>();

    try{
        BufferedReader inbuffer = new BufferedReader(new FileReader(fileName));
        String inputString;
        Stock aStock;
        // inputString = inbuffer.readLine();
        while ((line = inbuffer.readLine()) != null){
            row = new StringTokenizer(line, DELIMTER);
            if(row.countTokens() == 4){
                aStock = new Stock();
                aStock.setStockName(row.nextToken());
                aStock.setStockQuantity(Integer.parseInt(row.nextToken()));
                aStock.setPurchasePrice(Double.parseDouble(row.nextToken()));
                aStock.setCurrentPrice(Double.parseDouble(row.nextToken()));
                stockList.add(aStock);
            }
        }
        inbuffer.close();
    }
    catch(IOException ioe){
        JOptionPane.showMessageDialog(null, ioe.getMessage(), "File Read Error", JOptionPane.ERROR);
    }

    return stockList;
}

It also looks like you also need to add a loop in your loadFile() method to iterate through all of your stocks.

public void loadFile(){
    StockIO stockRead = new StockIO();
    jFileChooser1.showOpenDialog(jPanel3);
    File file = jFileChooser1.getSelectedFile();
    stockRead.loadFileName(file.getName());

    // Add all new stocks from getData to stockArr
    stockArr.addAll(stockRead.getData());

    // int index = 0;
    for (int index = 0; index < stockArr.length; index++) {
        if(stockArr.get(index) != null){
            DLM.addElement(stockArr.get(index).getStockName());
        }
    }

    listStock.setModel(DLM);

}
Sign up to request clarification or add additional context in comments.

5 Comments

oh yeah I get what you are saying. But my biggest problem is that I cant even read the second line of the file to send it to that point.
Yeah, you're right. I just took a quick glance and didn't notice. You should really just return an array of stocks from your getData() method and loop through all the lines with a while loop there.
@Zerdall, I made some edits. Take a look and let me know if you have any questions.
This worked so well! thank you so much I was unsure how to read multiple things. I am going to practice with the code you gave me and get used to it.
Awesome! Glad I could Help!

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.