-2
    import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.LayoutManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;


public class DataDisplay extends JFrame{
    JTable table;
    public static JLabel jl1= new JLabel("Activity ID");
    public static JTextField jt1= new JTextField("Enter Activity ID");

    public static JLabel jl2= new JLabel("Activity name");
    public static JTextField jt2 = new JTextField("Enter Activity name");

    public static JLabel jl3= new JLabel("Start Date");
    public static JTextField jt3= new JTextField("Enter Start Date");

    public static JLabel jl4 = new JLabel("End Date");
    public static JTextField jt4= new JTextField("Enter End Date");

    public static JLabel jl5 = new JLabel("Alarm Before");
    public static JTextField jt5= new JTextField("enter alarm");
    public static JLabel jl= new JLabel("HOURS");


    public static JButton save = new JButton("SAVE");
    public static JButton close = new JButton("CLOSE");
    public Container c= this.getContentPane();

    private static int counter=0;
    DataDisplay(){

        this.setTitle("Assignment 3");
        this.setLayout(null);
        this.setBackground(Color.MAGENTA);
        c.add(jl1);
        c.add(jt1);
        c.add(jl2);
        c.add(jt2);
        c.add(jl3);
        c.add(jt3);
        c.add(jl4);
        c.add(jt4);
        c.add(jl5);
        c.add(jt5);
        c.add(jl);


        jl1.setBounds(10, 10, 100, 100);
        jt1.setBounds(100, 45, 100, 30);

        jl2.setBounds(10,60,100,100);
        jt2.setBounds(100,100,120,30);

        jl3.setBounds(10,110,100,100);
        jt3.setBounds(100,145,120,30);

        jl4.setBounds(10,160,100,100);
        jt4.setBounds(100,200,120,30);

        jl5.setBounds(10,210,100,100);
        jt5.setBounds(100,250,120,30);
        jl.setBounds(230,210,100,100);


        c.add(save);
        save.setBounds(10,300,100,30);
        save.addActionListener(new ActionListener(){

            @Override
            public void actionPerformed(ActionEvent arg0) {
                int ce=0;
                // TODO Auto-generated method stub
                String tableHeader[] ={"Activity ID", "Activity name", "Start Date", "End Date","Alarm before (Hours)"};
                String tableData[][]= new String[ce=counter+1][10];
                table = new JTable(tableData, tableHeader);

                String line=null;

                BufferedReader b=null;


                try {
                    b = new BufferedReader(new FileReader("Activity.txt"));
                } catch (FileNotFoundException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                try {
                    int row=0,col=0;
                    while ((line=b.readLine())!=null){
                        String[] s= new String[100];



                        for(int counterc=1; counterc<=4; counterc++ ){//*ARRAY OUT OF BOUND EXCEPTION*
                         s=line.split("|",4);


                        }



                            tableData[row][0]=s[0];
                            tableData[row][1]=s[1];
                            tableData[row][2]=s[2];
                            tableData[row][3]=s[3];


                        row++;

                }} catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                finally{

                    try {
                        b.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }




                tableData[counter][0]=jt1.getText();
                tableData[counter][1]=jt2.getText();
                tableData[counter][2]=jt3.getText();
                tableData[counter][3]=jt4.getText();
                tableData[counter][4]=jt5.getText();


                JScrollPane scrollPane = new JScrollPane(table);
                c.add(scrollPane, BorderLayout.CENTER);
                scrollPane.setBounds(10, 400, 700, 600);
                counter++;
                try{
                    FileWriter out= new FileWriter("Activity.txt",true);
                    BufferedWriter br= new BufferedWriter(out);
                    PrintWriter p= new PrintWriter(br);
                    p.println(jt1.getText()+"|"+jt2.getText()+"|"+jt3.getText()+"|"+jt4.getText()+"|"+jt5.getText());
                    p.close();

                }
                catch(IOException e){
                    e.getStackTrace();

                }
            }

        });

        c.add(close);
        close.setBounds(200,300,100,30);

        this.setBounds(100,100,1000,1000);
        this.setVisible(true);
        c.setVisible(true);

    }

    public static void main(String args[]){

        DataDisplay dd= new DataDisplay();
    }

}

Here's my code. Only the last entered line in table is being displayed not the initial lines. I also used while(readline != null) but that gives either null pointer exception or array out of bounds exception. So what condition should I put in while loop?

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user3102085
  • 459
  • 3
  • 8
  • 19
  • 1
    possible duplicate of [Best way to read a text file](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file) – Alexandre Santos May 21 '14 at 17:50
  • An array out of bounds has nothing to do with reading files, it has everything to do with attempting to access an element or index of an array which is out of bounds. Ie. you are trying to read in more lines than you allowed for when you created the array. – Peter Lawrey May 21 '14 at 17:51
  • Can you give the text file you are trying to read? – ola May 21 '14 at 17:54
  • Also, you have one too many } on the fourth last line. "}} catch (IOException e) {" – Einar May 21 '14 at 17:56
  • @PeterLawrey Then why is it giving array out of bound exception even when i've seperated (split) my line by pipe operators like " Enter Activity ID|Enter Activity name|Enter Start Date|Enter End Date|enter alarm" and each string is stored in array in locations from 0 to 4? – user3102085 May 21 '14 at 17:59
  • Can you post a small, compilable example? It's hard to say why it only displays the last row when you leave out the code that displays. If you're seeing the last line repeated over and over, you've allocated `tableData` incorrectly. – that other guy May 21 '14 at 18:00
  • why you use the for to execute four times the same thing? – Marco Acierno May 21 '14 at 18:00
  • @user3102085 See my answer, you read more rows than you allowed for. – Peter Lawrey May 21 '14 at 18:11

5 Answers5

1

Is it bad form to suggest using IOUtils from apache commons?

LineIterator lineIterator = null;
try {
    lineIterator = FileUtils.lineIterator( new File( "" ) );
    while(lineIterator.hasNext()) {
        final String next = lineIterator.next();
        // do stuff with the line eg: 
        // eg: final String[] strings = StringUtils.split( next, '|' ); ...
    }
} catch( final IOException e ) {
    // use a logger to show error
}
finally {
    if( lineIterator != null ) {
        lineIterator.close();
    }
}
Christien Lomax
  • 105
  • 2
  • 5
0

I have changed your code in the following way:

    save.addActionListener(new ActionListener(){

        @Override
        public void actionPerformed(ActionEvent arg0) {
            int ce=0;
            // TODO Auto-generated method stub
            String tableHeader[] ={"Activity ID", "Activity name", "Start Date", "End Date","Alarm before (Hours)"};
            String tableData[][]= new String[ce=counter+1][5];
            table = new JTable(tableData, tableHeader);

            String line=null;

            BufferedReader b=null;


            try {
                b = new BufferedReader(new FileReader("Activity.txt"));
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            try {
                int row=0,col=0;
                line = b.readLine();
                    while (line != null) {
                        String[] s;

                        s = line.split("~");
                        for(int i = 0; i < s.length; i++) {
                            tableData[row][i]=s[i];
                        }
                    row++;
                    line = b.readLine();

            }} catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally{

                try {
                    b.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }




            tableData[counter][0]=jt1.getText();
            tableData[counter][1]=jt2.getText();
            tableData[counter][2]=jt3.getText();
            tableData[counter][3]=jt4.getText();
            tableData[counter][4]=jt5.getText();


            JScrollPane scrollPane = new JScrollPane(table);
            c.add(scrollPane, BorderLayout.CENTER);
            scrollPane.setBounds(10, 400, 700, 600);
            counter++;
            try{
                FileWriter out= new FileWriter("Activity.txt",true);
                BufferedWriter br= new BufferedWriter(out);
                PrintWriter p= new PrintWriter(br);
                p.println(jt1.getText()+"~"+jt2.getText()+"~"+jt3.getText()+"~"+jt4.getText()+"~"+jt5.getText());
                p.close();

            }
            catch(IOException e){
                e.getStackTrace();

            }
        }

    });

I changed the split character. I believe the pipe was causing some problems.

ola
  • 882
  • 2
  • 12
  • 29
  • It is being split but I can't display it on JTable(it gives array out of bound exception :/) "heres my code" while ((line=b.readLine())!=null){ String[] s= new String[100]; //*ARRAY OUT OF BOUND EXCEPTION* s=line.split("|"); for(int i = 0; i < s.length; i++) { System.out.println(s[i]); tableData[row][col]=s[i]; } – user3102085 May 21 '14 at 18:11
  • What is your end goal? As mentioned above, the tableData array is not being made large enough. – ola May 21 '14 at 18:16
  • Will you always be starting with a blank file? If you are not, you need to rethink your logic/ordering of creating the tableData. otherwise, this class should be fine. – ola May 21 '14 at 18:50
0
  1. Remember to close your BufferedReader in a finally block.
  2. readLine method will return null when the reader reached the end of the file. So you should edit your while condition to != null.
  3. Your for (int counterc = 1; counterc <= 4; counterc++) don't have a sense (or at least, i don't see it.)
  4. You need to split your line and then save it in the array.
  5. Why you have col ?
  6. Array s of 100 while you are storing only 4 elements?
  7. Split will create an array so the array you created with new String[4]; will be just losed. So move line.split in the same line.

    try {
        int row = 0, col = 0;
    
        // changed == null to != null
        while ((line = b.readLine()) != null) {
            String[] s = line.split("|", 4);
    
            tableData[row][0] = s[0];
            tableData[row][1] = s[1];
            tableData[row][2] = s[2];
            tableData[row][3] = s[3];
    
            row++;
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

Try it.

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
0

Only the last entered line in table is being displayed not the initial lines.

This is because you add to row outside of the while loop. Move the "}" after row++.

  • Hi and welcome to Stack Exchange. Your answer is very much appreciated but for many users it is often helpful if you can show exactly what you mean with a code solution. it can take two minutes of your time but save them a lot of problems. – Deepend May 21 '14 at 18:24
0

You have

String tableData[][]= new String[ce=counter+1][10];

where counter = 0. So you have enough space for 1 and only 1 row.

Later you read more than one row, so the program gets an ArrayOutOfBoundException.

If you don't know how many element you will have in advance, I suggest you use a List.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130