java hashmap

Discussion in 'Java' started by lakshmi2006, Apr 27, 2006.

  1. lakshmi2006

    lakshmi2006 New Member

    Joined:
    Apr 20, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hello sir ,
    To write a java program that parses a text file input.txt and the output of the program is a concurrent hashmap in the specified format.The below program makes use of vector data stucture to do this.Please modify this program so that the program should use hashmap data structure for parsing and outputing.please help me.
    Program.java:
    Code:
    import java.io.*;
    import org.w3c.dom.Document;
    import org.w3c.dom.*;
    import java.util.*;
    
    class ConfigLine
    {
    	public Vector ctrlRtrList = new Vector();
    	public Vector grpAddr = new Vector();
    	public Vector srcList = new Vector();
    	public Vector rtrList = new Vector();
    	public Vector rPSpecs = new Vector();
    
    	public void readData() throws IOException
    	{
    		BufferedReader filein = getFileStream();
    
    		if (filein == null)
    		return;
    
    
    		String currentLine = null;
    
    		// Read line by line
    		while((currentLine = filein.readLine()) != null )
    		{
    			// Ignore comments
    			if (currentLine.startsWith("#"))
    			continue;
    
    			else
    			{
    	StringTokenizer st=new StringTokenizer(currentLine, ":");
    
    				int i=0;
    				while (st.hasMoreElemen())
    				{
    		String tList(st.nextElement()).toString();
    					++i;
    					switch (i)
    					{
    					case 1:
    			fillVector(tList,ctrlRtrList); //Fill the vector with the list values
    						break;
    
    	                            case 2:grpAddr.addElement(tList);
    						break;
    
    		case 3:
                                          fillVector(tList,srcList);
    						break;
    
    		case 4:
    		fillVector(tList,rtrList);
    						break;
    
                                      case 5:
    			fillVector(tList,rPSpecs);
    						break;
    					
    }
    				}
    			}
    		}
    	}
    
    	public void fillVector(String listStr, Vector v)
    	{
    	StringTokenizer st1=new StringTokenizer(listStr, ",");
    		while (st1.hasMoreElements())
    		{
    			v.addElement(st1.nextElement());
    		}
    	}
    
    	// Gets the defaults file input stream as a buffered reader
    	private static BufferedReader getFileStream()
    	{
    		try
    		{
    	FileReader fr = new FileReader(new File("input.txt"));
    		BufferedReader br = new BufferedReader(fr);
    			return br;
    		}
    		catch(FileNotFoundException fnfeEx)
    		{
    			System.out.println(fnfeEx.toString());
    			return null;
    		}
    	}
    
    	public void readVector()
    	{
    		if(ctrlRtrList.size()==0)
    		System.out.println("\nNo Elements\n");
    		else
    		{
    		System.out.println("Control Router List:");
    			for(int i=0;i<ctrlRtrList.size();i++)
    		System.out.println(ctrlRtrList.elementAt(i));
    		}
    
    		if(grpAddr.size()==0)
    		System.out.println("\nNo Elements\n");
    		else
    		{
    		System.out.println("Group Address:");
    		System.out.println(grpAddr.elementAt(0));
    		}
    
    		if(srcList.size()==0)
    		System.out.println("No Elements\n");
    		else
    {
    			System.out.println("Source List:");
    			for(int i=0;i<srcList.size();i++)
    		System.out.println(srcList.elementAt(i));
    		}
    
    		if(rtrList.size()==0)
    		System.out.println("No Elements\n");
    		else
    		{
    			System.out.println("Router List:");
    			for(int i=0;i<rtrList.size();i++)
    		System.out.println(rtrList.elementAt(i));
    		}
    
    		if(rPSpecs.size()==0)
    		System.out.println("No Elements\n");
    		else
    		{
    			System.out.println("RP-Specs List:");
    			for(int i=0;i<rPSpecs.size();i++)
    		System.out.println(rPSpecs.elementAt(i));
    		}
    	}
    }
    
    
    class Program
    {
    	static ConfigLine cl=new ConfigLine();
    	public static void main(String argv[])
    	{
    		try
    		{
    			cl.readData();
    		}
    		catch(Exception e)
    		{
    		System.out.println("Wow u'v an exception");
    		}
    
    		cl.readVector();
    	}
    }
    
    input.txt:

    #Format:
    # <CtrRtrList>:<GrpAddr>:<SrcList>:<RtrList>[:RP-spec]
    10.2.32.82:239.192.0.2:10.00.5.183,10.0.6.185:10.2.32.85,10.2.32.81,10.2.32.82:10.0.2.82,,10.2.78.42
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    This forum is not for just putting your code and let ask to implement some more to it. Its for sharing of knowledge and ask your queries if you are stuck with something. If you are stuck with hashmap you can use the following links

    Hashmap
    Hashmap sample
     
  3. kush_2207

    kush_2207 New Member

    Joined:
    Jun 26, 2007
    Messages:
    49
    Likes Received:
    1
    Trophy Points:
    0
    What does hashmap ? Please explain.. It takes filename as an Input and what would be the output in the sense of hashmap ? Thank you.
     
  4. LauDauns

    LauDauns New Member

    Joined:
    Oct 11, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    I am sure you will find a good response!!
    Good luck for you.
     
    Last edited by a moderator: Oct 12, 2007
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    LauDauns, Please do not posts link to your site your account can be in trouble.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice