简体   繁体   English

Hashmap 问题在 Java 中从文件添加字符串

[英]Hashmap problem adding strings from a file in Java

I have a problem adding strings into hashmap in java.我在 java 中将字符串添加到 hashmap 时遇到问题。 My file has inside the following我的文件有以下内容

在此处输入图片说明

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.我设置了一个静态计数器“mapcounter”,它是地图的值,每次插入键后我都会增加一。 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".我用扫描仪打开文件,我们的文件指针是“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.有地图的键所以当我打印键 "30" 的值时,我得到正确的 0 ,因为它是第一个要添加的键。 when i check the value of key "1412" i get a number like 500+.当我检查键“1412”的值时,我得到一个像 500+ 这样的数字。 And i don't know why, also for key "3" i get value 6..... Any ideas??我不知道为什么,对于关键“3”,我得到价值 6..... 任何想法?

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.从 0 到 1,2,3,4,5,.....,1890,..... 等等。 Like that 30=0, 1412=1, 3352=2, 5254=3, 5543=4,7478=5,3=6, and so on.像这样 30=0, 1412=1, 3352=2, 5254=3, 5543=4,7478=5,3=6, 等等。 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这就是为什么我有词[2] 谢谢你的帮助

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.除了您的两个 next() 语句外,您的解决方案很好。 You need eliminate them and replace with a String.split to split the line into the two words.您需要消除它们并替换为String.split以将行拆分为两个单词。 The \\\\s+ is a regular expression that splits on one or more spaces. \\\\s+是一种在一个或多个空格上拆分的正则表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM