需要以某種方式獲取這兩個txt文件作爲我的鍵和值,我得到了第一個在列表工作,但需要從兩個txt文件映射所有單詞..我認爲即將發生的事情是我沒有正確解析文件。我能夠System.out.println(..)的內容,但我收到價值爲空,所以我在這裏失去了一些大事。反正新的程序員在這裏這是我的第一篇文章。您好! =)hashmap和多個txt文件java
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
//Create map structure **must have import
Map engJapDictionary = new HashMap<String,String>();
//user input
Scanner in = new Scanner(System.in);
System.out.print("Enter phrase for translation: ");
//name the user input
String name = in.next();
System.out.println("You wrote: " + name);
//Load Eng and Jap
String fileName = "word.txt";
String fileName1 = "word2.txt";
String line = null;
String line2= null;
try {
FileReader fileReader = new FileReader(fileName);
FileReader fileReader1 = new FileReader(fileName1);
BufferedReader bufferedReader = new BufferedReader(fileReader);
BufferedReader bufferedReader1 = new BufferedReader(fileReader1);
while ((line = bufferedReader.readLine()) != null) {
String parts[] = line.split("\t");
while ((line2 = bufferedReader1.readLine()) != null) {
String parts2[] = line2.split("\t");
engJapDictionary.put(parts[0], parts2[0]);
//for each entry in both txt files iterate through i think here. the .put works only for the first entry in both txt files.. =(
continue;
//System.out.println(engJapDictionary +"\n");
//System.out.println(line);
}
}
}catch (IOException e){}
//Size of the Dictionary!
System.out.println("The number of entries is: " + engJapDictionary.size());
// English to Japanese Dictionary
//Building dictionary the long way
engJapDictionary.put("me", "Watashi");
engJapDictionary.put("you", "anata");
engJapDictionary.put("hat", "boshi");
engJapDictionary.put("cat", "neko");
engJapDictionary.put("dream", "yume");
engJapDictionary.put("house", "uchi");
engJapDictionary.put("dog", "inu");
// System.out.println(engJapDictionary.get("you"));
// System.out.println(engJapDictionary.get("hat"));
// System.out.println(engJapDictionary.get("cat"));
// System.out.println(engJapDictionary.get("dream"));
// System.out.println(engJapDictionary.get("house"));
// System.out.println(engJapDictionary.get("dog"));
System.out.println("Japanese word: " + engJapDictionary.get(name));
System.out.println(engJapDictionary.get(name));
System.out.println(engJapDictionary.containsKey(name));
//Print the keys!!
//System.out.println("\n" + engJapDictionary.keySet());
//Print the values!!
//System.out.println(engJapDictionary.values());
}
}
地圖我敢打賭,你會看到錯誤沒有任何幫助。 http://idownvotedbecau.se/noattempt/ – blafasel
不認爲id很快就會被玷污。 =)但是我明白你的觀點,格式化非常重要..當intellij把我的東西帶過來時,控制k似乎不適合我。它只是得到了所有的廢棄.. –