2017-10-20 41 views
0

需要以某種方式獲取這兩個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()); 


    } 
} 
+1

地圖我敢打賭,你會看到錯誤沒有任何幫助。 http://idownvotedbecau.se/noattempt/ – blafasel

+0

不認爲id很快就會被玷污。 =)但是我明白你的觀點,格式化非常重要..當intellij把我的東西帶過來時,控制k似乎不適合我。它只是得到了所有的廢棄.. –

回答

2

您打電話engJapDictionary.put(parts[0], parts2[0]);爲第一個文件的第一個單詞與第二個文件的所有單詞。然後,您不要在Map中放置第一個文件的任何其他字,因爲您在內循環的第一次運行中迭代了第二個文件,因此line2 = bufferedReader1.readLine()返回null

你並不需要一個嵌套循環,只是一個循環讀取兩個文件在每個迭代的行:

while ((line = bufferedReader.readLine()) != null && (line2 = bufferedReader1.readLine()) != null) { 
    String parts[] = line.split("\t"); 
    String parts2[] = line2.split("\t"); 
    engJapDictionary.put(parts[0], parts2[0]); 
} 
+2

你是如此之快 –

+0

感謝您的快速解決..我會反思這= = –

0

伊蘭是正確爲止;一些額外的解釋:在外循環的第一次運行中,內循環將隨後用第二個文件的所有單詞替換第一個單詞,使得只有最後一個單詞將保留在地圖中。下一個循環運行將不會再在第二個文件中找到任何內容,因爲它已經被讀取,因此您可以獲得null值用於進一步的條目。

但我強烈推薦你另一種設計!這兩個文件的問題是你需要在兩個完全不同的位置管理數據。想象一下,你添加了一個新的單詞,但不小心你把它們放在兩個文件的不同行中–受影響的兩個索引之間的所有單詞都將被損壞......或者如果不添加單詞到其中一個單詞文件在所有?

優先列出條目,如一個單一的文件:

<english word>=<japanese word> 

那你都對應詞連在一起,你是從如上錯誤安全。您遍歷一個文件,通過"="分割線,如果你正確地格式化代碼填充

put(parts[0].trim(), parts[1].trim()); 
+0

不錯!它只有10k字,我會想出一種合併txt文件的方法,然後給它一個去..迄今爲止它的樂趣很多..!感謝朋友 –

+0

@ T.M。那麼,你可以使用你當前的代碼(當然是固定的變體):打開另一個文件(第三個文件),而不是將這些條目寫入到映射文件中:'file.printf(「%s =%s \ n「,parts [0] .trim(),parts [1] .trim());' – Aconcagua

+0

由於某種原因,我似乎無法利用file.printf,我只能解決這樣的問題。 System.out.printf(「%s =%s \ n」,parts [0] .trim(),parts2 [0] .trim()); –