Jump to content

Java Hashtable Problem


danieldrave

Recommended Posts

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

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

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.