Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Arraylist, jTable

Posted on 2010-09-14
18
Medium Priority
?
1,146 Views
Last Modified: 2012-05-10
Hello,
I have an arraylist and i'm trying to put the contents in a Jtable under column names First Name, Last Name, School ID, Number.

The code below extracts the values of First Name, Last Name, School ID, and Number from a file. I am trying to put these info in a Jtable but i don't know if and how to use the default table model or abstract table model.
Please Help.


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;




public class ReadFile {
	private static String output = "";
	//static List list = new ArrayList(); 
	 static ArrayList<String> list = new ArrayList<String>();
	
    public static void main(String[] args) {
  
        //
        // Create an instance of File for data.txt file.
        //
        File file = new File("data.txt");
        
         
      
        try {
            
            Scanner scanner = new Scanner(file);
           
            //output = "";
            while (scanner.hasNextLine()) {
                String str = scanner.nextLine();
                //System.out.println(line);
                if (parseString(str) != null) {
                // output += parseString(str);
                 list.add(parseString(str));
                       
                } 
               
            }
            
            System.out.println(list);
            System.out.println();
            
            for(int i=0;i<list.size();i++)
            {
            System.out.println(list.get(i));
            }

           

            //System.out.println(list);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        
    }
    
    private static String parseString(String line) {
    
        if (line.contains("First Name") || line.contains("Last Name") || line.contains("School ID")|| line.contains("Number")) {
                    line = line.substring(line.indexOf("=") + 1, line.length());
            //return line + "\n";
            return line;
           
        	
        } else {
            return null;
        }
    } 
    
 }

Open in new window

0
Comment
Question by:Smock
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 2
18 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 33675507
Please post example file. CSV is a good way
0
 

Author Comment

by:Smock
ID: 33675585
I have a txt file with the following:

ID = 0
First Name=Joe
Last Name=Doe
Age=21
Gender=Male
School = middle school
StudentID = 1234
Address = some address.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33675621
That's not a good format. After one entry, the 'column names' become redundant and wasteful. I would use CSV. You can then use

http://technojeeves.com/joomla/index.php/free/104-csv-to-tablemodel
0
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:Smock
ID: 33675625
Thanks but it's in a text file and not a csv.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33675672
Then split on '=' and create a DefaultTableModel. After the first data 'row' you'll have to ignore the column names
0
 

Author Comment

by:Smock
ID: 33675735
My code splits on '=' and stores the values in an arraylist. I don't know how to put it in the Jtable.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33675834
Is it always just one row of data?
0
 

Author Comment

by:Smock
ID: 33676001
I'm writing a bigger program and will incoporate multiple rows.

My goal is to eventually loop through a bunch of files named data.txt and extract the necessary data and then store it in the jtable.
so, it's not always one row of data.
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 100 total points
ID: 33676074
Use Vector instead of ArrayList. Every row must be one Vector instance, which must be added to an 'allRows' Vector. Then you can use a DefaultTableModel

http://download.oracle.com/javase/6/docs/api/javax/swing/table/DefaultTableModel.html#DefaultTableModel(java.util.Vector,%20java.util.Vector) 
0
 

Author Comment

by:Smock
ID: 33676156
So do i store the output that is split from the "=" into a Vector?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33676268
Yes, you add each column of a single row to one and the same Vector
0
 

Author Comment

by:Smock
ID: 33676940
May i be given an example
0
 
LVL 92

Assisted Solution

by:objects
objects earned 400 total points
ID: 33677549
Use the Properties class to parse the file


Properties props = new Properties();
FileInputStream in = new FileInputStream("data.txt");
props.load(in);
in.close();

String id = props.getString("ID");
...

Then add the data to your table model

0
 
LVL 92

Accepted Solution

by:
objects earned 400 total points
ID: 33677566
tableModel.addRow(new Object[] { props.getString("ID"), props.getString("First Name"), props.getString("Last Name") });
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33680055
Using Properties will only work for a single row btw.

Your problem here is that your input format is not a good one (see http:#33675621 ), so you've made things difficult for yourself
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33949524
Plenty of help given
0
 

Author Closing Comment

by:Smock
ID: 33978331
thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question