Results 1 to 3 of 3
  1. #1
    behrk2 is offline Member
    Join Date
    Jul 2008
    Posts
    4
    Rep Power
    0

    Default Question about hash tables

    Hey all,

    I am looking to do something like this:

    Java Code:
    Hashtable board = new Hashtable ();
    			
    Integer red = new Integer (0);
    Integer ora = new Integer (0);
    Integer yel = new Integer (0);
    Integer gre = new Integer (0);
    Integer blu = new Integer (0);
    Integer pur = new Integer (0);
    
     for(int k = 0; k < 4; k++)
             {
                String let = useCode[k];
                char ch = let.charAt(0);
             
                switch(ch)
                {
                   case 'R': board.put("Red", (red+1));
                      break;
                   case 'O': board.put("Orange", (ora+1));
                      break;
                   case 'Y': board.put("Yellow", (yel+1));
                      break;
                   case 'G': board.put("Green", (gre+1)); 
                      break;
                   case 'B': board.put("Blue", (blu+1)); 
                      break;
                   case 'P': board.put("Purple", (pur+1));
                      break;
                   default: cEmpty++;
                }
             }
    However, I don't think my syntax is right, as my integer variables (red, ora, yel, etc.) do not change from 0.

    Can anyone offer any insight? Thanks!

  2. #2
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    The values of variables red, ora, yel,etc.. wont chage in the your code becoz you just add value 1 with that variable and put it in the hashtable. so the hashtable contains incremented value but not the variable. If you want to increment the variable value too then use the following code.. I just make changes in the swith block of your code:

    Java Code:
    import java.io.*;
    import java.util.Hashtable;
    import java.util.Enumeration;
    
    public class Colours
    {
    		public static void main(String [] args)
    		{
    
    			try
    			{
    				Hashtable board = new Hashtable ();
    
    				String [] useCode ={"Red", "Orange", "Yellow", "Green", "Blue", "Pur"};
    
    				Integer red = new Integer (0);
    				Integer ora = new Integer (0);
    				Integer yel = new Integer (0);
    				Integer gre = new Integer (0);
    				Integer blu = new Integer (0);
    				Integer pur = new Integer (0);
    
    				int cEmpty=0;
    
    				for(int k = 0; k < 5; k++)
    				{
    					String let = useCode[k];
    					char ch = let.charAt(0);
    
    					System.out.println("Ch = " + ch);
    
    					switch(ch)
    					{
    						case 'R':
    					   		board.put("Red", (++red));
    						  	break;
    
    					   case 'O':
    					   		board.put("Orange", (++ora));
    						  	break;
    
    					   case 'Y':
    					   		board.put("Yellow", (++yel));
    						  	break;
    
    					   case 'G':
    					   		board.put("Green", (++gre));
    						  	break;
    
    					   case 'B':
    					   		board.put("Blue", (++blu));
    						  	break;
    
    					   case 'P':
    					   		board.put("Purple", (++pur));
    					   		break;
    
    					   default:
    					   		cEmpty++;
    					}
    				 }
    
    
    				 System.out.println("Key \t Value");
    
    				 Enumeration keyCollection = board.keys();
    
    				 while(keyCollection.hasMoreElements())
    				 {
    					String keyname = (String)keyCollection.nextElement();
    					int value = (Integer)board.get(keyname);
    
    					System.out.println(keyname+"\t" + value);
    				 }
    
    				
    
    
    			}
    			catch(Exception e)
    			{
    				e.printStackTrace();
    			}
    		}
    }
    Last edited by baskar.nitt; 07-08-2008 at 09:34 AM.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Please post a link to the other site(s) where you have posted this question so we'll not answer a question already answered on the other site.

Similar Threads

  1. Hash table with double hashing
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:43 PM
  2. Hash table with linear probing
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:43 PM
  3. Hash table with separate chaining
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-12-2008, 09:42 PM
  4. Hash Table help
    By rhm54 in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 02:25 AM
  5. caching the tables
    By jayashree in forum JDBC
    Replies: 0
    Last Post: 01-30-2008, 07: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
  •