User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,515 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,797 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 457 | Replies: 0 | Solved
Reply
Join Date: Sep 2007
Posts: 9
Reputation: Icetigris is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Icetigris's Avatar
Icetigris Icetigris is offline Offline
Newbie Poster

Bizzare logic error

  #1  
Sep 30th, 2007
This compiles, runs fine, and generally works, except for one thing. The contains() method doesn't work properly. For some reason table.contains(key) isn't returning the right value, which is thereby messing up the insert() method.

  1. public class HashtableDictionary implements StringDictionaryAlgorithm
  2. {
  3. static final boolean debug = false;
  4. Hashtable table = new Hashtable();
  5. int value = 0;
  6. /*
  7. .
  8. .
  9. .
  10. */
  11.  
  12. public boolean contains(java.lang.String key)
  13. {
  14. boolean isItThere = true;
  15. isItThere = table.containsKey(key);
  16. return isItThere;
  17. }
  18.  
  19. /*
  20. .
  21. .
  22. .
  23. */
  24.  
  25. public boolean insert(java.lang.String key)
  26. {
  27. boolean didItAdd = true;
  28. boolean duplicate = true;
  29.  
  30. duplicate = table.contains(key);
  31.  
  32. if(duplicate == true)
  33. {
  34. didItAdd = false;
  35. }
  36. else if(duplicate == false)
  37. {
  38. Object whut = new Object();
  39. whut = table.put(key, value);
  40. whut = table.get(key);
  41. value++;
  42. if(whut == null)
  43. {
  44. didItAdd = false;
  45. }
  46. }
  47. return didItAdd;
  48. }
  49.  
  50. public static void main(String[] args)
  51. {
  52. System.out.println("Here's what you want to use as keys in the hashtable: ");
  53.  
  54. for (int i=0; i<args.length; i++)
  55. {
  56. System.out.print(args[i] + " ");
  57. }
  58. System.out.print("\n");
  59.  
  60. HashtableDictionary dictionary = new HashtableDictionary();
  61. boolean added = true;
  62. boolean contains = false;
  63.  
  64. for (int i=0; i<args.length; i++) //ADD THINGS TO LIST
  65. {
  66. added = dictionary.insert(args[i]);
  67.  
  68. if(added == false)
  69. {
  70. System.out.println("something didn't add right: " + args[i]);
  71. }
  72. }
  73.  
  74. System.out.println("Your table:");
  75. System.out.println(dictionary.table.toString());
  76.  
  77. String whatToFind = "dongs";
  78.  
  79. contains = dictionary.contains(whatToFind);
  80.  
  81. if(contains == false)
  82. {
  83. System.out.println("string isn't in the list: " + whatToFind);
  84. }
  85. else if(contains == true)
  86. {
  87. System.out.println("string is in the list: " + whatToFind);
  88. }
  89.  
  90.  
  91. }
  92.  
  93. }

Any ideas? Thanks

EDIT:
DISREGARD THAT;
I just realised I should have used containsKey() not contains().
Last edited by Icetigris : Sep 30th, 2007 at 8:22 pm. Reason: Fixed it myself
AddThis Social Bookmark Button
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 3:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC