danieldrave Posted October 20, 2011 Posted October 20, 2011 Hi Guys, I'm having a problem with the results I'm getting from my Hashtable code! It basically only prints out the last key and value I create in the fileSave() method. Here's source code: import java.util.*; import java.io.*; /** * HashTable Class * @author Daniel Drave * Version 1.0 */ public class HashTable { /* fileSave & fileLoad METHODS TO GO HERE, * THE TWO METHODS WILL HANDLE THE LOADING & SAVING OF THE SAMPLE .TXT FILE * IT WILL THEN BE LINKED TO THE A METHOD IN THE MAIN METHOD WHICH WILL COMPARE AND MATCH * WORDS FROM THE .TXT FILE TO THE OLIVER TWIST SAMPLE TEXT AND DISPLAY THE LINE NUMBERS * WHERE THE WORDS ARE IN OLIVER TWIST */ private static void fileSave() { /* WE MAKE A NEW HASHTABLE */ Hashtable <String, Object> hashtable = new Hashtable<String, Object>(); /* NOW WE'LL PUT IN SOME SAMPLE STRINGS */ hashtable.put("string", "tomorrow"); hashtable.put("string", "food"); hashtable.put("string", "coat"); hashtable.put("string", "outside"); /* WE'LL NOW USE A LARGE TRY & CATCH MEHTOD TO WRITE THE FILE TO OUR PROGRAM */ try{ System.out.println("Attempting creation of File Output Stream..."); FileOutputStream fileOut = new FileOutputStream("SampleText.txt"); ObjectOutputStream out = new ObjectOutputStream (fileOut); System.out.println("Attempting to write Hashtable Object..."); out.writeObject(hashtable); /* HASHTABLE RELATING TO THE ONE WE CREATED EARLIER */ System.out.println("Now closing all Output Streams..."); out.close(); fileOut.close(); } /* THROW IN SOME EXCEPTIONS IN CASE THE .TXT IS NOT FOUND */ catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } } private static void fileLoad() { /* WE SET THE HASHTABLE TO NULL AS WE'RE GOING TO APPLY SOME KEYS & VALUES TO IT LATER */ Hashtable<String, Object> hashtable = null; try { System.out.println("Creating File/Object Stream..."); FileInputStream fileIn = new FileInputStream("SampleText.txt"); ObjectInputStream in = new ObjectInputStream(fileIn); System.out.println("Loading File/Object Stream..."); hashtable = (Hashtable<String, Object>)in.readObject(); System.out.println("Attempting to close Stream..."); in.close(); fileIn.close(); } catch(ClassNotFoundException e) { e.printStackTrace(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } System.out.println("Printing out loaded elements..."); for(Enumeration<String> e = hashtable.keys(); e.hasMoreElements(){ Object obj = e.nextElement(); System.out.println("- Element(" + obj + ") = " + hashtable.get(obj) + "\n"); } } public static void main(String args[]) throws IllegalArgumentException { fileSave(); fileLoad(); } } And the result:... Attempting creation of File Output Stream...Attempting to write Hashtable Object... Now closing all Output Streams... Creating File/Object Stream... Loading File/Object Stream... Attempting to close Stream... Printing out loaded elements... - Element(string) = outside Any help you could provide would be great! Thanks Guys, Dan
alextui Posted October 20, 2011 Posted October 20, 2011 Change your class name to HashTable1( rather than HashTable) and try again.
danieldrave Posted October 20, 2011 Author Posted October 20, 2011 I changed it to public class Hashtable1 and the output still hasn't changed :/
alextui Posted October 20, 2011 Posted October 20, 2011 (edited) Change this: for(Enumeration<String> e = hashtable.keys(); e.hasMoreElements(){ Object obj = e.nextElement(); System.out.println("- Element(" + obj + ") = " + hashtable.get(obj) + "\n"); } into this: for(String key = hashtable.keyset()){ Object obj = hashtable.get(key); System.out.println("- Element(" + key + ") = " + obj + "\n"); } Edited October 20, 2011 by alextui
danieldrave Posted October 20, 2011 Author Posted October 20, 2011 Switched the code to your recommended source code: - The method keyset() is undefined for the type Hashtable<String,Object> That's the error message I'm getting! Is keyset() linked to HashTables or HashMaps because I'm not allowed to use HashMaps =]
alextui Posted October 20, 2011 Posted October 20, 2011 u need to learn solved the simple problem by looking at java doc. here: http://download.oracle.com/javase/6/docs/api/java/util/Hashtable.html then you will find out , it is keySet(), not keyset(). we can't spoon feeding here, the admin of this forum have zero tolerance on spoon feeding.
danieldrave Posted October 20, 2011 Author Posted October 20, 2011 Its fine I understand just getting a bit frustrated with my code, thanks for the link!
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now