•
•
•
•
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
![]() |
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.
Any ideas? Thanks
EDIT:
DISREGARD THAT;
I just realised I should have used containsKey() not contains().
java Syntax (Toggle Plain Text)
public class HashtableDictionary implements StringDictionaryAlgorithm { static final boolean debug = false; Hashtable table = new Hashtable(); int value = 0; /* . . . */ public boolean contains(java.lang.String key) { boolean isItThere = true; isItThere = table.containsKey(key); return isItThere; } /* . . . */ public boolean insert(java.lang.String key) { boolean didItAdd = true; boolean duplicate = true; duplicate = table.contains(key); if(duplicate == true) { didItAdd = false; } else if(duplicate == false) { Object whut = new Object(); whut = table.put(key, value); whut = table.get(key); value++; if(whut == null) { didItAdd = false; } } return didItAdd; } public static void main(String[] args) { System.out.println("Here's what you want to use as keys in the hashtable: "); for (int i=0; i<args.length; i++) { System.out.print(args[i] + " "); } System.out.print("\n"); HashtableDictionary dictionary = new HashtableDictionary(); boolean added = true; boolean contains = false; for (int i=0; i<args.length; i++) //ADD THINGS TO LIST { added = dictionary.insert(args[i]); if(added == false) { System.out.println("something didn't add right: " + args[i]); } } System.out.println("Your table:"); System.out.println(dictionary.table.toString()); String whatToFind = "dongs"; contains = dictionary.contains(whatToFind); if(contains == false) { System.out.println("string isn't in the list: " + whatToFind); } else if(contains == true) { System.out.println("string is in the list: " + whatToFind); } } }
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
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Logic Error? (Need Help, Getting Exception Part II lol) (Java)
- Logic error? (C++)
- Erm, logic error?! (C++)
- compile error (C)
- Switch logic error (C++)
- Error comparing strings from arrays (PHP)
- Could someone please help me with this logic error?? (VB.NET)
Other Threads in the Java Forum
- Previous Thread: Basic For Loop Program HELP
- Next Thread: User Input without GUI... Need Guidance


Linear Mode