-1

I have a hash map and an ArrayList. Both of them are populated as I have tried to print them out and it works fine. The arrayList contains MeterNumbers (MeterNumber is the key of the HashMap). The map contains MeterNumbers for keys and String for values.

What I want to be able to do is, get the String value from the the hasMap given the MeterNumber key which i will provide from the ArrayList. I don't think I need to check if it exists there coz I know it does for sure.I have tried all I can to get the value but it keeps giving me null values. Here is my code.

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Try {  
    static Map <MeterNumber, String> map2 = new HashMap <MeterNumber, String>();
    static ArrayList<MeterNumber> blackOutMeters = new ArrayList<MeterNumber>();

    public static void main (String args[]) {

        try {
            Scanner sc2 = new Scanner(new java.io.File("meters.txt"));
            Scanner sc3 = new Scanner(new java.io.File("outages.txt")); 
            while (sc2.hasNextLine()) {
                String transformerId;
                MeterNumber meterId;                
                String line = sc2.nextLine();
                String[] array = line.split(" ");               
                if (array.length>3){
                    transformerId = array[3];
                    meterId = MeterNumber.fromString(array [0] + array [1] + array [2]);
                    map2.put(meterId, transformerId);                   
                }
            }
    //      System.out.println (map2.values());
            while (sc3.hasNextLine()) {
                MeterNumber meterId;                
                String line = sc3.nextLine();
                String[] array = line.split(" ");               
                if (array.length>2){                
                    meterId = MeterNumber.fromString(array [0] + array [1] + array [2]);        
                    blackOutMeters.add(meterId);                                                    
                }               
            }

        for (int i = 0; i <blackOutMeters.size(); i++){

            String s = map2.get(blackOutMeters.get(i));
            System.out.println (s);

        }


} 
        catch (FileNotFoundException e) {

            e.printStackTrace();
        }
    }}

file format for meters.txt is:

900 791 330 T1
379 165 846 T1
791 995 073 T1
342 138 557 T1
114 125 972 T1
970 324 636 T1
133 997 798 T1
308 684 630 T1
169 329 493 T1
540 085 209 T1
265 229 117 T1
970 173 664 T1
264 943 573 T1
462 043 136 T1
087 307 071 T1
001 343 243 T1

file format for outages.txt is:

900 791 330 
379 165 846 
791 995 073 
342 138 557 
114 125 972 
970 324 636 
133 997 798 

Thank you in advance.

lolian
  • 15
  • 6
  • Where's the code for `MeterNumber`? – Brendan Long Nov 08 '14 at 02:49
  • The reason is that 1) no item for the given key was found, which usually indicates a bad equals/hashCode or 2) a null was assigned as a value. It is not productive to approach computer problems with "No Reason". – user2864740 Nov 08 '14 at 02:49
  • (And if it is related to the equals/hashCode there are plenty of duplicates..) – user2864740 Nov 08 '14 at 02:50
  • http://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java?lq=1 , http://stackoverflow.com/questions/1894377/understanding-the-workings-of-equals-and-hashcode-in-a-hashmap?lq=1 – user2864740 Nov 08 '14 at 02:53

1 Answers1

4

You need to implement hashCode and equals for MeterNumber

Otherwise Java has no way of knowing how to compare your objects

Eric
  • 19,525
  • 19
  • 84
  • 147
  • Hi, thank you. I am not sure how to do what you said. Can explain a bit more. thanks – lolian Nov 08 '14 at 03:04
  • [`hashCode`](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()) and [`equals`](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object)) are methods that are defined in [`Object`](http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html). You need to override those methods in your class `MeterNumber` as those are used to compare your objects. – mkobit Nov 08 '14 at 03:12
  • @MikeKobit Great! Thank you guys. I just did a bit of research on that. Thank you – lolian Nov 08 '14 at 03:22