Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-08-2008, 06:28 AM
Member
 
Join Date: Jul 2008
Posts: 4
behrk2 is on a distinguished road
Question about hash tables
Hey all,

I am looking to do something like this:

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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-08-2008, 09:26 AM
baskar.nitt's Avatar
Member
 
Join Date: Apr 2008
Location: Chennai, India
Posts: 15
baskar.nitt is on a distinguished road
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:

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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-08-2008, 05:40 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 854
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hash table with double hashing Java Tip java.lang 0 04-12-2008 09:43 PM
Hash table with linear probing Java Tip java.lang 0 04-12-2008 09:43 PM
Hash table with separate chaining Java Tip java.lang 0 04-12-2008 09:42 PM
Hash Table help rhm54 New To Java 0 02-08-2008 02:25 AM
caching the tables jayashree Database 0 01-30-2008 07:43 AM


All times are GMT +3. The time now is 05:46 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org