无法从密钥中获取值(HashMaps,文本文件)

时间:2016-05-23 09:32:17

标签: java dictionary hashmap java.util.scanner

我最近开始编码,我已经被困了好几天了。 所以我需要从“key”获得“value”,这在表格中如下:

    one uno
    two dos
    three tres
    four cuatro

N.B。:单词由制表符分隔,因此使用“/ t”

我能成就的新手看起来像这样:

    import java.util.*;
    import java.io.*;
    public class test {
       public static void main(String[] args)throws FileNotFoundException, IOException {  
    Scanner scanner = new Scanner(new FileReader("C:\\Users\\Admin\\workspace\\test_dict\\src\\source.txt"));

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

        while (scanner.hasNextLine()) {
            String[] columns = scanner.nextLine().split("\t");

            map.put(columns[0],(columns[1]));
//enter a key word
   System.out.println("Enter a word");
   String[] columns = scanner.nextLine();
//ignoring upper and lower case
          columns=columns.toLowerCase();
// returns the element associated with the key
           System.out.println(map.get(columns[])); 

    //close files
       scanner.close();
        }
       }
    }

问题是:我希望能够在控制台中键入关键字并获得相关值。例如:

Enter a word:
    one
    uno

但是我不能使用map.get(columns []); 当我因某种奇怪的原因尝试map.get(columns[0]);时,它会向我显示columns[1]所以在控制台中我得到了

Enter a word:
uno
Enter a word:
dos
Enter a word:
tres
Enter a word:
cuatro

2 个答案:

答案 0 :(得分:0)

     scanner.nextLine();

nextLine()的输出是字符串而不是字符串数组,所以你需要像这样做

    String consoleInput = scanner.nextLine();
    System.out.println(map.get(consoleInput.toLowerCase())); 

答案 1 :(得分:0)

有几个问题:

  • 您尝试在单个while语句中读取文件和输入。因此,您的翻译将无法完整导入,您已尝试在其中进行搜索。
  • 类型不安全,因为您使用扫描仪变量来读取文件&#34;和#34;控制台输入。

这里有一个例子,我试过的方法想要更清楚地解决它:

import java.util.*;
import java.io.*;

public class Test {
    public static void main(String[] args)throws Exception {

        Scanner scanner = new Scanner(new FileReader("C:\\Users\\Admin\\workspace\\test_dict\\src\\source.txt"));

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

        while (scanner.hasNextLine()) {
            // you are loading a String Array from file to into "columns[]"
            String[] columns = scanner.nextLine().split("\t");

            // The array of the line is splittet in inserted in your map
            map.put(columns[0],(columns[1]));

            // That is all, you have to do the input in the outside of your reading while statement

            //close files
            scanner.close();
        }

        // Using input from console: http://stackoverflow.com/questions/4644415/java-how-to-get-input-from-system-console
        String input = null;
        while ((input = getConsoleInput("Enter a word: ")) != null) {
            // Use only the input for key-Search
            System.out.println(map.get(input));
        }


    }

    // Helper method for reading input
    public static String getConsoleInput(String message) throws  IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print(message);
        String input = br.readLine();
        return input;
    }
}