Ant*_*rth 5 java string null dictionary map
我有一个非常不寻常的问题.
基本上,我正在尝试从字符串映射到字符串获取与键相关联的值.
我知道我正在使用的钥匙存在; 我使用的是与之相同的字符串!
我在我的代码中打印语句,这就是我的情况......
这是我的字典characterDictionary
{thusChar=?, spaceChar= , plusChar=+, equalsIndent=#, multiplyChar=×, equalsChar==, newlineChar=\n, divideChar=÷, subjectChar=:, variableIndent=@}
Run Code Online (Sandbox Code Playgroud)
最后一个关键"variableIndent"就是麻烦!
这是代码......
System.out.println ( characterDictionary.get("variableIndent") );
Run Code Online (Sandbox Code Playgroud)
不恰当地输出: null
我已检查,双重检查并三重检查我的代码.
键"variableIndent"与字符串参数之间绝对没有区别characterDictionary.get("variableIndent"),但它的行为就好像这个键不存在一样.
我绝对可以保证这个密钥存在,并且两个字符串是相同的.
所有其他元素(我检查过的那些;到目前为止大约3个)都是正常检索的.为什么"variableIndent"的"@"值正在播放?
您可能会注意到该字典包含非ASCII字符,例如"soChar".这有关系吗?
谢谢
(这似乎是一个非常简单和微不足道的问题,好像我犯了一些可怜的愚蠢错误,但我却无法解决它!)
编辑:
好的,这是关于编码的东西.
我从字典中取出字符串键并将其与我的get参数进行比较.
打印时,它们是相同的,但java说它们并不相同.
键字符串来自UTF-8编码的文本文件,而参数字符串来自java Eclipse文字.
但是字符是相同的.
问题是什么,我该如何解决?
谢谢!
编辑:
嗯,这就是幕后实际发生的事情.
我有一个UTF-8文本文件,其中包含以下内容......
variableIndent,@
equalsIndent,#
spaceChar,
newlineChar,\n
multiplyChar,×
divideChar,÷
plusChar,+
equalsChar,=
subjectChar,:
thusChar,?
Run Code Online (Sandbox Code Playgroud)
我ArrayList<String>通过传递文件的目录,通过读取文件的每一行作为元素'加载'这个文件:
private static ArrayList<String> readLinesFile(String ResourceFile) {
ArrayList<String> Lines = new ArrayList<String>();
try{
InputStream fstream = FileManager.class.getResourceAsStream(ResourceFile);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
while ((strLine = br.readLine()) != null) {
Lines.add(strLine); }
in.close(); }
catch (Exception e) {
System.out.println("Error: " + e.getMessage()); }
return Lines; }
Run Code Online (Sandbox Code Playgroud)
这里的一切都很好.
然后我将这个ArrayList传递给一个函数,该函数用","字符分割每个元素(使用个人包中的函数;绝对不是问题),并将第一部分作为第二部分添加到新的第二部分中字典.
private static Map<String, String> generateBaseUnits_Characters_Prefixes(ArrayList<String> FileContent) {
Map<String, String> BaseUnitsCache = new HashMap<String, String>();
ArrayList<String> currentLine;
for (int i=0; i<FileContent.size(); i++) {
currentLine = MiscellaneousFunctions.splitString(FileContent.get(i), ",");
BaseUnitsCache.put(currentLine.get(0), currentLine.get(1)); }
return BaseUnitsCache; }
Run Code Online (Sandbox Code Playgroud)
这会产生导致所有麻烦的字典.
我有一组与文本文件中的字符名称相对应的Key Literals,我用它来访问程序中的字典.
public static String variableIndentKey = "variableIndent";
public static String equalsIndentKey = "equalsIndent";
public static String spaceCharKey = "spaceChar";
public static String newlineCharKey = "newlineChar";
public static String multiplyCharKey = "multiplyChar";
public static String divideCharKey = "divideChar";
public static String plusCharKey = "plusChar";
public static String equalsCharKey = "equalsChar";
public static String subjectCharKey = "subjectChar";
public static String thusCharKey = "thusChar";
Run Code Online (Sandbox Code Playgroud)
这是问题所在:
文本文件的顶行在字典中"搞砸了".它被添加到字典中并正确显示在keySet和字典的打印格式中,但尝试访问它会返回"null".
(在这种情况下,它是variableIndent.如果我将variableIndent放在文本文件中的其他位置,则equalsIndent拧紧等等)
这是怎么回事!?
我有狡猾的功能吗?!
他们对其他一切都很好.
谢谢!
将包含密钥和值的UTF-8文本文件更改为不带BOM的UTF-8.在"variableIndent"之前有三个字节(UTF-8 BOM 0xEF,0xBB,0xBF),请参阅http://en.wikipedia.org/wiki/Byte_order_mark
| 归档时间: |
|
| 查看次数: |
2894 次 |
| 最近记录: |