Results 1 to 6 of 6
  1. #1
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Post 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:
    Java Code:
    Steve 22
    Harry 19
    Ben 29
    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:
    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();
           }
    
            }
    I can test this code by doing the following:
    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());
    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.

    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:

    Java Code:
     String search = (String) ma.hashMap.get("Steve");
            System.out.println(search);
    
    System.out.println ( "Contains the name  ="+hashmap.containsKey ( "Harry" )  ) ;
    Sorry about the long post, just im slightly confused why it isn't working :confused:

  2. #2
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default

    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:
    Java Code:
    Scanner scanner = new Scanner(file);
               while (scanner.hasNext()) {
                   if (scanner.hasNext()){
                       line = scanner.nextLine();
    When it should be:
    Java Code:
    Scanner scanner = new Scanner(file);
               while (scanner.hasNext()) {
                   if (scanner.hasNext()){
                       line = scanner.next();
    All this because a four letter word! grrrr

  3. #3
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    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 :)

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I agree with adz

  5. #5
    mainy is offline Member
    Join Date
    Feb 2009
    Posts
    38
    Rep Power
    0

    Default

    Thanks. I will add in generics, but may I ask what is there purpose?

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    I not only explicitly states the data types for the programmer, it eliminates the need to cast the results, since java already knows the data type contained in the table.

    It also prevents people from putting objects of the wrong type into the table, which is possible if generics are not used.

Similar Threads

  1. HashMap contains all values but doesn't show all values
    By xcallmejudasx in forum New To Java
    Replies: 3
    Last Post: 05-11-2009, 12:35 AM
  2. HashMap: Obtaining all values in a collision?
    By markus-sukram in forum New To Java
    Replies: 2
    Last Post: 03-29-2008, 11:25 PM
  3. How to make a hashmap to allow duplicate values?
    By Preethi in forum New To Java
    Replies: 0
    Last Post: 02-08-2008, 01:35 PM
  4. how to return values from hashmap
    By oregon in forum New To Java
    Replies: 2
    Last Post: 08-01-2007, 05:56 PM
  5. Problems with Find method in EJB
    By Nick15 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 05-14-2007, 02:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •