Results 1 to 6 of 6
- 07-28-2009, 08:55 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Problems with hashMap, has values in it but can't find them?
Hello, once again :p
This is an extended problem from a previous post, but decided to create a new thread as it is an error with hashMaps and not Scanner.
I have a file with this kind of information:
I have, with assistance from this site managed to work out how to place the contents of a text file into a HashMap. I have put this into the class Main (just for testing purposes).Java Code:Steve 22 Harry 19 Ben 29
I can test this code by doing the following:Java Code:public String line = new String(); public int point = 0; public String pointS = new String(); public HashMap hashMap = new HashMap(); public Main() { File file = new File("C:/Users/Kerby/Documents/NetBeansProjects/test/src/test/test.txt"); try { Scanner scanner = new Scanner(file); while (scanner.hasNext()) { if (scanner.hasNext()){ line = scanner.nextLine(); } // All text in the file is placed into a variable called line. if (scanner.hasNextInt()){ point = scanner.nextInt(); pointS = Integer.toString(point); } // All numbers found in the file is converted to a string and placed into a variable called pointS. hashMap.put(line, pointS); // line and pointS values are placed as pairs into the hashMap. } } catch (FileNotFoundException e) { e.printStackTrace(); } }
This shows that there is in fact three pairs stored inside the HashMap, it shows the the HashMap is not empty, AND it even displays all the keys and values inside the HashMap. This proves it does have all the information from the file inside it.Java Code:Main ma = new Main(); System.out.println("HashMap contains " + ma.hashMap.size() + " pairs"); System.out.println ( "If hashmap empty = "+ ma.hashMap.isEmpty ()) ; Set st = ma.hashMap.keySet(); Iterator itr = st.iterator(); while(itr.hasNext()) System.out.println(itr.next());
BUT! When I try to search for a value or key inside using many different method it just returns "null", stating it cant find that value/key even though by using the previous pieces of code shows that it is in there.
These are some examples of different methods I have tried using to display keys and their relevant values:
Sorry about the long post, just im slightly confused why it isn't working :confused:Java Code:String search = (String) ma.hashMap.get("Steve"); System.out.println(search); System.out.println ( "Contains the name ="+hashmap.containsKey ( "Harry" ) ) ;
- 07-28-2009, 08:58 PM #2
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
haha, Im such a dumbass. I just figured out what the problem was from re-reading through this post to check for errors!
I had in the scanner code:
When it should be:Java Code:Scanner scanner = new Scanner(file); while (scanner.hasNext()) { if (scanner.hasNext()){ line = scanner.nextLine();
All this because a four letter word! grrrrJava Code:Scanner scanner = new Scanner(file); while (scanner.hasNext()) { if (scanner.hasNext()){ line = scanner.next();
- 07-28-2009, 09:10 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 35
- Rep Power
- 0
Just to point out you should use generics,such as:
HashMap<String, Integer> map = new HashMap<String, Integer>();
And your iteration could be easily shortened to:
for (Map.Entry<String, Integer> entry : map.entrySet()) {
...
}
or something to that effect :)
- 07-28-2009, 09:42 PM #4
I agree with adz
- 07-28-2009, 09:52 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 38
- Rep Power
- 0
Thanks. I will add in generics, but may I ask what is there purpose?
- 07-28-2009, 10:22 PM #6
Similar Threads
-
HashMap contains all values but doesn't show all values
By xcallmejudasx in forum New To JavaReplies: 3Last Post: 05-10-2009, 11:35 PM -
HashMap: Obtaining all values in a collision?
By markus-sukram in forum New To JavaReplies: 2Last Post: 03-29-2008, 10:25 PM -
How to make a hashmap to allow duplicate values?
By Preethi in forum New To JavaReplies: 0Last Post: 02-08-2008, 12:35 PM -
how to return values from hashmap
By oregon in forum New To JavaReplies: 2Last Post: 08-01-2007, 04:56 PM -
Problems with Find method in EJB
By Nick15 in forum Enterprise JavaBeans (EJB)Replies: 0Last Post: 05-14-2007, 01:29 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks