【问题标题】:Can not acquire value from Hashtable无法从 Hashtable 获取值
【发布时间】:2016-09-26 00:30:32
【问题描述】:

我写了下面的Java代码,

try {
    String str = "";
    Hashtable< String, String> table = new Hashtable< String, String>();
    fis = new FileInputStream("C:\\Users\\Dave\\Desktop\\station.txt");// FileInputStream
    isr = new InputStreamReader(fis); 
    br = new BufferedReader(isr); 

    String str1 = "012649-99999";
    String str2 = "012650-99999";
    while ((str = br.readLine()) != null) {
        String[] record = str.split("\t");
        table.put(record[0], record[1]);
    }           
    String stationName1 = table.get(str1);
    String stationName2 = table.get(str2);

} catch(...)

station.txt的内容如下:

012649-99999    SIHCCAJAVRI
012650-99999    TRNSET-HANSMOEN

当我运行程序时,stationName1 始终为空,而 stationName2 可以得到值012650-99999。谁能告诉我为什么会这样?提前谢谢!

@matt:是的,没错,当我将编码从“UTF-8”更改为“ANSI”时,它起作用了,stationName1 可以获取值,但是为什么“UTF-8”不适用于这种情况?我总是使用这种格式。

【问题讨论】:

  • 你应该为一件事使用 HashMap。填充地图时如何打印记录 [0] 和记录 [1]。确保里面发生的事情是你认为的那样。
  • 1) 使用HashMap&lt;String, String&gt;。 2) 编程到interface 所以Map&lt;X,Y&gt; = new HashMap&lt;&gt;()。 3) 你为什么不打印Map 的内容来看看里面有什么?您可能需要trim()
  • 我可以确定record[0]和record[1]是我想的和我想要的,我已经打印到控制台了。
  • 如果它打印出你想要的东西,那么你的错误就在其他地方。您还可以检查record[0].equals(str2); 它应该等于这些输入。否则,您已经遗漏了太多问题,没有人可以帮助您。你在哪里检查 null?
  • 根据您的编辑,听起来您的文件没有正确分成几行。您可能需要检查文件编码/行尾。

标签: java regex string split hashmap


【解决方案1】:

问题是您的文本文件不包含任何\t 字符。有多个空格。正确的方法是使用\\s+,匹配多个空格。

String[] record = str.split("\\s+");

此外,Hashtable 已过时。现在有HashMap&lt;&gt;。这是为我工作的完整代码。我已经测试过了:

String str;
HashMap<String, String> table = new HashMap<>();
FileInputStream fis = new FileInputStream("station.txt");
InputStreamReader isr = new InputStreamReader(fis); 
BufferedReader br = new BufferedReader(isr); 

String str1 = "012649-99999";
String str2 = "012650-99999";

while ((str = br.readLine()) != null) {
    String[] record = str.split("\\s+");
    table.put(record[0], record[1]);
}           
System.out.println(table.get(str1));
System.out.println(table.get(str2));

【讨论】:

  • 我刚才试过代码String[] record = str.split("\\s+");但是结果是一样的。
  • 我现在已经尝试过并且为我工作。请参阅上面的完整代码。它根本不能为空。您是否从正确的文件中读取?
  • 是的,文件格式不正确,当我将编码从'UTF-8'更改为'ANSI'时,它起作用了,stationName1可以获取值,但是为什么'UTF-8'没有为这种情况工作?我总是使用这种格式。
  • @Coinning,你需要了解什么是编码。 UTF-8 在这里工作得很好......当输入文件是 UTF-8 编码时。但是您的文件是 ANSI 文件,而不是 UTF8。这就是为什么。
【解决方案2】:

能否请您替换以下行:

 String[] record = str.split("\t");

通过以下行:

String[] record = str.split("[\\s]+");

看看结果?

您的工作解决方案在这里:-

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Hashtable;

public class Test {
    public static void main(String[] args) {
        try {
            String str = "";
            Hashtable< String, String> table = new Hashtable< String, String>();
            FileInputStream fis = new FileInputStream("C:\\Users\\Dave\\Desktop\\station.txt");// FileInputStream

            InputStreamReader isr = new InputStreamReader(fis); 
            BufferedReader br = new BufferedReader(isr); 

            String str1 = "012649-99999";
            String str2 = "012650-99999";
            while ((str = br.readLine()) != null) {
                System.out.println(str);
                String[] record = str.split("[\\s]+");
                table.put(record[0], record[1]);
            } 
            br.close();
            String stationName1 = table.get(str1);
            String stationName2 = table.get(str2);

            System.out.println("stationName1:"+stationName1);//
            System.out.println("stationName2:"+stationName2);//
        } catch(Exception e){
            System.out.println(e);
        }
    }

}

【讨论】:

    【解决方案3】:

    如果您确定文件包含 TAB 空格,则正确的匹配方法如下。

    String[] record = str.split("\\t");  
    

    split() 的参数是一个正则表达式,TAB 空间的正则表达式是 \t,它作为 Java 字符串将是 "\\t"

    另外,不要使用Hashtable,而是使用HashMap,如另一个答案中所述。

    【讨论】:

    • 只用一个斜线分割也可以。当然,两个斜线更好
    • 我不是反对者,但它也不起作用。
    猜你喜欢
    • 1970-01-01
    • 2020-02-20
    • 1970-01-01
    • 2016-08-23
    • 1970-01-01
    • 2011-02-24
    • 2022-01-10
    • 2020-12-24
    • 2018-12-09
    相关资源
    最近更新 更多