Search code examples
javahashmapkey-value-store

HashMap keeps returning null value for no obvious reason


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.


Solution

  • You need to implement hashCode and equals for MeterNumber

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