Results 1 to 5 of 5
Thread: hashtable
- 01-20-2011, 02:10 PM #1
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
hashtable
Hi
my task is to read a csv file in the following format and display it in a jtable
for which i want to store it first in a datastructure so that i can filter it depending on the columnnames
csv file looks like:
Anton;12;male;ABC
Martha;13;femal;ZXY
so i read each line and want to store in a hashtable
following code is what i got from java help
but since i must read a row a time in the csv file i cannot enter all the names,ages,sexes, schools details as shown below.
is there a way to , read a line and store it in hashtable or other datastructure with the keys as column names ?
Java Code:MyTableModel mtm = null; String[] columnNames = {"Name", "Age", "Sex", "School"}; Hashtable model = new Hashtable(columnNames.length); Object[][] dataOnCols = { {"Anton","Martha"}, //col 0 values {12,13}, //col 1 values {"male","female"}, //col 2 values {"ABC","ZXY"} }; for (int j = 0; j < columnNames.length; j++) { String columnName = columnNames[j]; model.put(columnName, dataOnCols[j]); } mtm = new MyTableModel(model); }
-
Simple: create a class whose objects hold each "row" of data.
- 01-20-2011, 02:21 PM #3
Hi, You can use List<Map<String, String>> constructions
for example
Java Code:import java.util.*; public class ListDataStore { public ListDataStore() { } public List<String> headers() { List<String> header = new ArrayList<String>(); header.add("number"); header.add("name"); header.add("aliec"); header.add("category"); header.add("deadline"); header.add("budget"); return header; } public List<Map<String, String>> data() { List<Map<String, String>> data = new ArrayList<Map<String, String>>(); for (int i=0;i!=10;++i) { Map<String, String> row = new HashMap<String, String>(); for (String head : headers()) { row.put(head, "value"); } data.add(row); } return data; } public static void main(String[] args) { ListDataStore store = new ListDataStore(); for (Map<String, String> map : store.data()) { for (String header : store.headers()) { System.out.print("head : " + header +"; value : " + map.get(header)); } System.out.println(); } } }Skype: petrarsentev
http://TrackStudio.com
- 01-20-2011, 04:02 PM #4
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
hashtable
hi thanx for your replies
for which i get the following outputJava Code:public class MyTable { static Hashtable fastTable; public ListDataStore() { } public static void addRow(String Name,String Age,String Sex,String School) { String[] columnNames = {"Name","Age","Sex","School"}; fastTable = new Hashtable(columnNames.length); Vector<Object> row = new Vector<Object>(); row.add(Name); row.add(Age); row.add(Sex); row.add(School); for(int i = 0; i < columnNames.length; i++){ Object rowVal = row.get(i); if(rowVal != null){ fastTable.put(columnNames[i],rowVal); }else{ fastTable.put(columnNames[i],""); } } } public static void main(String[] args) { for (int i = 0 ; i < 5 ; i++) { addRow("Martha","13","female","AMC"); } for (int i = 0 ; i < 5 ; i++) { addRow("Anton","12","Male","XYZ"); } // Get Hashtable Enumeration to get key and value Enumeration em=fastTable.keys(); while(em.hasMoreElements()) { //nextElement is used to get key of Hashtable String key = (String)em.nextElement(); //get is used to get value of key in Hashtable String value=(String)fastTable.get(key); System.out.println("Key :"+key+" value :"+value); } } }
Key :Sex value :Male
Key :Name value :Anton
Key :School value :XYZ
Key :Age value :12
Why is it that the first entry is missing ( i am new to java )
regards
- 01-20-2011, 04:24 PM #5
Member
- Join Date
- May 2010
- Posts
- 47
- Rep Power
- 0
Similar Threads
-
Problem with Hashtable key.
By XmisterIS in forum New To JavaReplies: 7Last Post: 09-11-2010, 08:19 AM -
hashtable
By vijayabaskar in forum Java ServletReplies: 0Last Post: 04-06-2009, 08:20 AM -
hashtable
By vijayabaskar in forum Advanced JavaReplies: 2Last Post: 04-06-2009, 08:05 AM -
Hashtable
By angelicsign in forum New To JavaReplies: 6Last Post: 02-05-2009, 04:30 PM -
Hashtable example
By Java Tip in forum Java TipReplies: 0Last Post: 02-15-2008, 08:43 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks