Smock
asked on
Arraylist, jTable
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.
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;
}
}
}
Please post example file. CSV is a good way
ASKER
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.
ID = 0
First Name=Joe
Last Name=Doe
Age=21
Gender=Male
School = middle school
StudentID = 1234
Address = some address.
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
http://technojeeves.com/joomla/index.php/free/104-csv-to-tablemodel
ASKER
Thanks but it's in a text file and not a csv.
Then split on '=' and create a DefaultTableModel. After the first data 'row' you'll have to ignore the column names
ASKER
My code splits on '=' and stores the values in an arraylist. I don't know how to put it in the Jtable.
Is it always just one row of data?
ASKER
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So do i store the output that is split from the "=" into a Vector?
Yes, you add each column of a single row to one and the same Vector
ASKER
May i be given an example
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Your problem here is that your input format is not a good one (see http:#33675621 ), so you've made things difficult for yourself
Plenty of help given
ASKER
thanks