Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8364369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:24:53+00:00 2026-06-09T12:24:53+00:00

I need to load .txt to JTable with delimiter , I found a good

  • 0

I need to load .txt to JTable with delimiter, I found a good sample here

This is the sample data :

 102|Beth Reiser||New York|(212)5558725
 111|Dylan Ricci||Syracuse|(315)5554486
 116|Brian Gugliuzza||Mamaroneck|(914)5553817
 120|Gertrude Stein||Elmsford|(914)5553476
 131|Daljit Sinnot||Bohemia|(516)5559811

This is my modified code :

package Model;

import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import java.io.*;
import java.util.*;

public class DataFileTableModel extends AbstractTableModel {

    protected Vector data;
    protected Vector columnNames ;
    protected String datafile;

    public DataFileTableModel(String f, String delimiter){
        datafile = f;
        initVectors(delimiter);
    }

    public void initVectors(String delimiter) {

        String aLine ;
        data = new Vector();
        columnNames = new Vector();
        int lineNum=0;

        try {
            FileInputStream fin =  new FileInputStream(datafile);
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));
            // extract column names
            StringTokenizer st1 =
                    new StringTokenizer(br.readLine(), delimiter);
            while(st1.hasMoreTokens())
                columnNames.addElement(st1.nextToken());
            // extract data
            while ((aLine = br.readLine()) != null && lineNum<20) {
                StringTokenizer st2 =
                        new StringTokenizer(aLine, delimiter);
                lineNum++;
                while(st2.hasMoreTokens())
                    data.addElement(st2.nextToken());
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public int getRowCount() {
        return data.size() / getColumnCount();
    }

    public int getColumnCount(){
        return columnNames.size();
    }

    public String getColumnName(int columnIndex) {
        String colName = "";

        if (columnIndex <= getColumnCount())
            colName = (String)columnNames.elementAt(columnIndex);

        return colName;
    }

    public Class getColumnClass(int columnIndex){
        return String.class;
    }

    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        return (String)data.elementAt( (rowIndex * getColumnCount()) + columnIndex);
    }

    public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
        return;
    }
}

According to data, it will display 5 column, but it just display 4 column, column 3 were skipped because of empty. I want to display all column to JTable, how could I achieve this? According to @hovercraft-full-of-eels it’s successfully display when the empty column is in the middle of the table, but it cannot handle the empty column in the back side. How to get this?

Data sample for empty column in the back side:

102|Beth Reiser||New York|(212)5558725||||
111|Dylan Ricci||Syracuse|(315)5554486||||
116|Brian Gugliuzza||Mamaroneck|(914)5553817||||
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:24:54+00:00Added an answer on June 9, 2026 at 12:24 pm

    It’s all due to your using a StringTokenizer: Your tokenizer is skipping the empty column pure and simple. First you should simplify your problem — test the StringTokenizer’s parsing of the text file without any Swing code, and then try to fix it before adding any Swing code or making your JTable.

    Check out the results of testing your data using a StringTokenizer vs. splitting on \\|:

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
    
    public class DataFileFoo {
       private static final String DATA_FILE_PATH = "datafile.txt";
       private static final String DELIMITER = "|";
       private static final String SPLIT_REGEX = "\\|";
    
       public static void main(String[] args) {
          System.out.println("usingStringTokenizer()");
          usingStringTokenizer();
    
          System.out.println();
          System.out.println("usingStringSplit();");
          usingStringSplit();
       }
    
       public static void usingStringTokenizer() {
          File datafile = new File(DATA_FILE_PATH);
          try {
             FileInputStream fin = new FileInputStream(datafile);
             BufferedReader br = new BufferedReader(new InputStreamReader(fin));
             // extract column names
             String aLine = "";
             while ((aLine = br.readLine()) != null) {
                StringTokenizer st2 = new StringTokenizer(aLine, DELIMITER);
                while (st2.hasMoreTokens()) {
                   System.out.print(st2.nextToken() + ", ");
                }
                System.out.println();
    
             }
             br.close();
          } catch (Exception e) {
             e.printStackTrace();
          }
       }
    
       public static void usingStringSplit() {
          File datafile = new File(DATA_FILE_PATH);
          try {
             FileInputStream fin = new FileInputStream(datafile);
             BufferedReader br = new BufferedReader(new InputStreamReader(fin));
             // extract column names
             String aLine = "";
             while ((aLine = br.readLine()) != null) {
                String[] tokens = aLine.split(SPLIT_REGEX);
                for (String token : tokens) {
                   System.out.print(token + ", ");
                }
                System.out.println();
    
             }
             br.close();
          } catch (Exception e) {
             e.printStackTrace();
          }
       }
    }
    

    Using your data file,

     102|Beth Reiser||New York|(212)5558725
     111|Dylan Ricci||Syracuse|(315)5554486
     116|Brian Gugliuzza||Mamaroneck|(914)5553817
     120|Gertrude Stein||Elmsford|(914)5553476
     131|Daljit Sinnot||Bohemia|(516)5559811
    

    This returns:

    usingStringTokenizer()
     102, Beth Reiser, New York, (212)5558725, 
     111, Dylan Ricci, Syracuse, (315)5554486, 
     116, Brian Gugliuzza, Mamaroneck, (914)5553817, 
     120, Gertrude Stein, Elmsford, (914)5553476, 
     131, Daljit Sinnot, Bohemia, (516)5559811, 
    
    usingStringSplit();
     102, Beth Reiser, , New York, (212)5558725, 
     111, Dylan Ricci, , Syracuse, (315)5554486, 
     116, Brian Gugliuzza, , Mamaroneck, (914)5553817, 
     120, Gertrude Stein, , Elmsford, (914)5553476, 
     131, Daljit Sinnot, , Bohemia, (516)5559811, 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to load data from source table to Flat file detination(txt). For example
I need to load a Model in a component to save the Data of
I need to load data into my treestore. My ajax request give me XML
I need to load a lot of data fetched from a mysql db in
Please help me out with this. I have this small application to load txt
i need to load the third column of this text file as a hex
I need to load a couple of thousands of data files into SQL Server
I'm new to scripting and need a lot of help understanding how to load
I need to Load RVM into a shell session as a function , so
I need to load a custom function from an extern file but without causing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.