Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 806
  • Last Modified:

Loading a tab delimited file into JTable

Hey,
      I want to read tab delimited data from a file into a JTable, line by line.First line is for columns. However i'm having difficulty using the split function of String.I am unsure what to use because  split("\\t") or split(" ") causes the data to load improperly. How can i achieve this?
0
D0cX
Asked:
D0cX
  • 3
1 Solution
 
CEHJCommented:
You need to make a TableModel

                  Vector allRows = new Vector();
                  // for each line:
                  allRows.add(new Vector(Arrays.asList(line.split("\\s*?"))));
                        // then
                  JTable table = new JTable(new DefaultTableModel(allRows));
0
 
CEHJCommented:
Forgot this:

// (for first line)
Vector headers = new Vector(Arrays.asList(line.split("\\s*?")));

// then

 JTable table = new JTable(new DefaultTableModel(allRows, headers));
0
 
D0cXAuthor Commented:
Hey thanks for the help.
It produced more desirable results , however for a file with data

col1 col2 col3 col4
dat1 dat2 dat3 dat4

i get a leading empty column before the first data column and blank columns in between data columns. Any ideas on how to refine this?
what does "\\s*?" do anyway?
0
 
CEHJCommented:
Hmm. Any chance of posting part of the file somewhere publicly downloadable?

>>what does "\\s*?" do anyway?

Tokenizes on whitespace
0
 
aozarovCommented:
You can try:
line.trim().split("\\s+");

e.g.
bsh % Arrays.asList("    col1 col2 col3    col4   ".trim().split("\\s+"));
<[col1, col2, col3, col4]>
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now