Jump to content

Java Hashtable Problem

Featured Replies

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

 

 

 

 

 

 

 

 

Change your class name to HashTable1( rather than HashTable) and try again.

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 by alextui

  • Author

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 =]

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.