Search code examples
javahashmap

Hashmap problem adding strings from a file in Java


I have a problem adding strings into hashmap in java. My file has inside the following

enter image description here

On top it has comments and after that it has two number in each line. I want to remove comments or just ignore them and set each number into the hashmap only once, so i have to check for reappearance. I wrote the code below but i have problems with counting. SOMETIMES it's random. I have set a static counter "mapcounter" that is the value of the map that after every key insertion i increase by one. Here is the code:

    String line = filemap.nextLine();
    while(filemap.hasNextLine()) {

        if(!line.startsWith("#")) {
            String word[]=new String[2];
            word[0]=filemap.next();
            word[1]=filemap.next();

            if(!map.containsKey(word[0])){
                map.put(word[0],mapcounter++);
            }
            if(!map.containsKey(word[1])){
                map.put(word[1],mapcounter++);
            }
        }
        line = filemap.nextLine();
    }

Some other details. I open the file with scanner and our file pointer is "filemap". i treat the numbers as Strings ! There are the keys of the map So when i print the value of the key "30" i get correct 0 , as it is the first key to add. when i check the value of key "1412" i get a number like 500+. And i don't know why, also for key "3" i get value 6..... Any ideas??

Extra explanation: i want to keep each different Number from both columns into the hash map so i can arrange them a value. From 0 to 1,2,3,4,5,.......,1890,..... and so on. Like that 30=0, 1412=1, 3352=2, 5254=3, 5543=4,7478=5,3=6, and so on. If a key appears again i want to ignore it because already has a value. Also i want to keep and from the second column, not just the first one. That's why i have word[2] Thanks for your help


Solution

  • Base on new information provided in comments.

    Given a

     Map<String, Integer> map = new HashMap<>();
    

    You can read in two values and update the counter based on their occurrence

            int mapcounter = 0;
            String line = filemap.nextLine();
            while(filemap.hasNextLine()) {
                if(!line.startsWith("#")) {
    
                   // split the two node column into two values.
                    String[] words = line.split("\\s+");
    
                    if(!map.containsKey(word[0])){
                        map.put(word[0],mapcounter++);
                    }
                    if(!map.containsKey(word[1])){
                        map.put(word[1],mapcounter++);
                    }
                }
                line = filemap.nextLine();
            }
    

    In my first answer, I totally misunderstood what you wanted. Your solution was fine except for your two next() statements. You need eliminate them and replace with a String.split to split the line into the two words. The \\s+ is a regular expression that splits on one or more spaces.