Hi guys,
I'm making a class for importing a table from a text file with tab delimited data.
It works perfectly as long as there are no empty cells in the text file.

(see attached example)

Any suggestions how to detect an empty cell and what to fill the ArrayList with if the cell is empty?

public class ImportFileTableModel extends AbstractTableModel {

    protected ArrayList data;
    protected ArrayList columnNames ;
    protected String datafile;
    public ImportFileTableModel(String f){
        datafile = f;
    public void initArrayLists() {
        String aLine ;
        data = new ArrayList();
        columnNames = new ArrayList();
        try {
            FileInputStream fin =  new FileInputStream(datafile);
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));
            // extract column names
            StringTokenizer st1 = new StringTokenizer(br.readLine(), "\t");
            while(st1.hasMoreTokens()) columnNames.add(st1.nextToken());
            // extract data
            while ((aLine = br.readLine()) != null) {
                StringTokenizer st2 = new StringTokenizer(aLine, "\t");
                while(st2.hasMoreTokens()) data.add(st2.nextToken());
        } catch (Exception e) {
 public int getRowCount() {
        return data.size() / getColumnCount();
    public int getColumnCount(){
        return columnNames.size();
Re: Importing a table with a few empty cells 80 80

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

(API doc for StringTokeniser)

You should do a lot better by reading one whole line at a time then splitting it (String split method) on the tab delimiters to get a String array. I haven't tested this but you should get array elements of "" where there are consecutive tabs, although it may be better to trim() the elements to get rid of any spurious blanks.

Re: Importing a table with a few empty cells 80 80

Thanks James, the String split method works!

Re: Importing a table with a few empty cells 80 80

OK, excellent! Mark this one solved. See you next time ;-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.