Results 1 to 5 of 5

Thread: hashtable

  1. #1
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default 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);
    
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    618
    Rep Power
    4

    Default

    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

  4. #4
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default hashtable

    hi thanx for your replies

    Java 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);
            }
    
        }
    }
    for which i get the following output

    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

  5. #5
    kmm1977 is offline Member
    Join Date
    May 2010
    Posts
    47
    Rep Power
    0

    Default hashtable

    what i wan to achieve is something like this

    -----------------------------
    Name Age Sex School
    -----------------------------
    Anton 12 male ABC
    Martha 13 female XYZ

    this datastructure must be show up as a JTable in which i must be able to filter based on the columnnames

Similar Threads

  1. Problem with Hashtable key.
    By XmisterIS in forum New To Java
    Replies: 7
    Last Post: 09-11-2010, 08:19 AM
  2. hashtable
    By vijayabaskar in forum Java Servlet
    Replies: 0
    Last Post: 04-06-2009, 08:20 AM
  3. hashtable
    By vijayabaskar in forum Advanced Java
    Replies: 2
    Last Post: 04-06-2009, 08:05 AM
  4. Hashtable
    By angelicsign in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 04:30 PM
  5. Hashtable example
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-15-2008, 08:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •