简体   繁体   English

Java Map匹配密钥

[英]Java Map Matching Key

i am facing a problem which is: I have map containing string and string. 我面临的问题是:我的地图包含字符串和字符串。 When i print that map i can see that there is a key "0-8166-3835-7". 当我打印该地图时,我可以看到有一个键“ 0-8166-3835-7”。 But when i am trying to get it, there is nothing to get returned, like no matching found... My code: 但是,当我尝试获取它时,没有任何东西可以返回,就像找不到匹配项...我的代码:

    //Open a stream to read from file with isbn's AND titles
    Scanner IsbnTitle = new Scanner(new FileReader("C:/Users/Proskopos/Documents/NetBeansProjects/ReadUrl/IsbnTitle.txt"));

    //Create a Map to save both ISBN's and Titles
    Map <String,String> IsbnTitleMap = new HashMap();

    while(IsbnTitle.hasNext()){
        String recordIsbnTitle = IsbnTitle.nextLine();
        UrlFunctions.AddToMap(Recognised , recordIsbnTitle, IsbnTitleMap);
    }
.....
.....
        Set IsbnSet = new HashSet();
    while (IsbnFile.hasNextLine()) {
        String isbn = IsbnFile.nextLine();
        IsbnSet.add(isbn);
    }

    //Create an Iterator for IsbnSet
    Iterator IsbnIt =IsbnSet.iterator();

String suffix = IsbnIt.next().toString();

    String OPACIALtitle = UrlFunctions.GetOpacTitle(suffix, IsbnTitleMap);

The code above is the only part in main about MAP and below are the functions i call: 上面的代码是MAP中main的唯一部分,下面是我调用的函数:

    static String GetOpacTitle(String opIsbn, Map IsbnTitle) {
    String OpacTitle = null;
    String isbn = opIsbn;
    Map isbnMap = IsbnTitle;
    System.out.println(isbn);
    if ( isbnMap.containsKey(isbn)){
        System.out.println("AAAAAAAAAAAAAAAA");
    }
    //String tade = isbnMap.get(isbn).toString();
    //System.out.println("*************" + tade);

    return OpacTitle;
}


    static void AddToMap(int Recognise, String recordIsbnfollowed, Map IsbnfollowedMap) {

    Map isbnsth = IsbnfollowedMap;
    String records = recordIsbnfollowed;
    int recs= Recognise;


    if (recs == 0 || recs == 3) {
    String isbn = records.substring(0, 10);
    String title = records.substring(10);
    isbnsth.put(isbn, title);
//        System.out.println(isbn);
        }else if (recs == 1) {
            String isbn = records.substring(0, 14);
            String title = records.substring(14);
            isbnsth.put(isbn, title);
//            System.out.println(isbn);
        }
    }

I cant understand where the problem is.. Maybe it is something like encoding of the suffix cames from a set, and the key from a map? 我无法理解问题出在哪里。也许后缀的编码来自一组,而键来自地图? they are both string.. dont think so..!!! 他们都是字符串..不这么认为.. !!! So? 所以? Can you help? 你能帮我吗?

EDIT: I am trully sorry if you find the code difficult to read :\\ I will follow your advices!! 编辑:如果您觉得代码难以阅读,我将深表歉意:\\我将按照您的建议! BUT in case that anyone else has the same problem the solution was what Brand said below.. (I re-post it) 但是,如果其他人遇到相同的问题,解决方案将是布兰德在下面说的..(我重新发布)

You probably have some whitespace in the Strings that you are reading form the file and storing in the Map. 您可能正在从文件读取并存储在Map中的字符串中包含一些空格。 If this is the case use String.trim() before storing the value in the Map, and also before querying for the same string. 如果是这种情况,请在将值存储在Map中之前以及在查询同一字符串之前使用String.trim()。 – Brad 3 hours ago –布拉德3小时前

Thank you all 谢谢你们

Just to add to my comment that identified the problem. 只是要添加到我的发现问题的评论中。 When comparing Keys in a Map you must be very careful about white space and case sensitivity. 在比较地图中的按键时,您必须非常注意空白和区分大小写。 These types of issues commonly occure when reading data from Files because it's not always obvious what characters are being read. 这些类型的问题通常是在从文件中读取数据时发生的,因为并非总是很清楚正在读取哪些字符。 Even looking in your debugger whitepsace cna be an issue. 即使在调试器中查看whitepsace cna也是一个问题。

Always try to "normalize" your data by trimming leading and trailing whitespace before storing it in a Map. 在将数据存储在地图中之前,请始终尝试通过修剪前导和尾随空白来“标准化”数据。

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

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