1
try {
    final List<String> ar = new ArrayList<String>();
    final PRIvariable pri = new PRIvariable();

    final BufferedReader reader = new BufferedReader(
        new InputStreamReader(new FileInputStream("C:/cdr2.csv")));

    while (reader.ready()) {
        final String line = reader.readLine();
        final String[] values = line.split(",");
        pri.dateText = values[2] + "    " + values[4];
        pri.count = pri.count + 1;
        pri.sum = pri.sum + Integer.parseInt(values[7]);
        System.out.println(pri.dateText + "  " + pri.sum + " " + pri.count);
        ar.add(pri);
    }

    final String[] columnNames = { "Date", "TOTAL", "COUNTS" };
    final String[][] cells = new String[ar.size()][3];
    for (int i = 0; i < ar.size(); i++) {
        cells[i][0] = ((PRIvariable) ar.get(i)).dateText;
        cells[i][1] = "" + ((PRIvariable) ar.get(i)).sum;
        cells[i][2] = "" + ((PRIvariable) ar.get(i)).count;
    }
    table = new JTable(cells, columnNames);
    table.setSize(400, 400);
    table.setVisible(true);
    final JScrollPane js = new JScrollPane();
    js.setViewportView(table);
    js.setSize(400, 400);
    js.setVisible(true);
    add(js, java.awt.BorderLayout.CENTER);

} catch (final Exception e) {
    System.out.println(e);
}

This is my code. Here i want to Read text file and put that data to Jtable. But in this code it shows every row of the Jtable filled with same data that contain in arraylist(ar) last row. ( i think there is problem in my arraylist). How can i solve this......

3
  • 2
    HAPPY NEW YEAR 2011............... Commented Jan 1, 2011 at 6:33
  • +1 finally some interesing question! Commented Jan 1, 2011 at 9:34
  • -1, Quit cluttering the forum. You where given the exact same answer yesterday when you posted the same question: stackoverflow.com/questions/4562920/…. This is a waste of time for everybody who has read this question since you aleady had the answer one day ago. If you didn't understand the answers given then add a follow up comment. Don't create a new posting, keep all the infomation in one place so everybody knows what has already been suggested!!! Commented Jan 1, 2011 at 16:45

1 Answer 1

2

The problem is with the variable pri. It has to be created inside the while loop.

Like this

String line = null;
while ((line = reader.readLine()) != null) {
    PRIvariable pri = new PRIvariable();
    String[] values = line.split(",");
    pri.dateText = values[2] + "    " + values[4];
    pri.count = pri.count + 1;
    pri.sum = pri.sum + Integer.parseInt(values[7]);
    System.out.println(pri.dateText + "  " + pri.sum + " " + pri.count);
    ar.add(pri);
}

In your code your want to create a separate instance of PRIvariable for every line in the file, but you are creating only once instance of PRIvariable at the beginning then you are always using that instance by overriding the previous value.

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

Comments

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.