我迷路了,需要指導。我寫了我的代碼來掃描book.txt,但是我怎樣才能將input.txt中的單詞與book.txt中的單詞進行比較?讀取.txt文件以查看是否在不同的.txt文件中找到了單詞?
回答
這樣的工作,我覺得
Scanner scan = new Scanner("book.txt");
Set<String> list = new HashSet<String>();
String word = "";
while(scan.hasNextLine())
list.add(scan.nextLine());
scan.close();
scan = new Scanner("input.txt");
while(scan.hasNextLine())
if(!list.contains((word = scan.nextLine()))) //word from input.txt is not in book.txt
System.out.println(word); //print the word to the console
scan.close();
謝謝你的回答。不幸的是,我們不能使用Set
你在同一行有多個單詞嗎?爲什麼設置
如果您不能使用一個HashSet然後創建一個屬於自己的hashFunction。
大多采用散列函數是這樣的:
public static int mod = 1000007;
private static Long hashFunction(String word) {
long hash = 5831;
for (int i = 0; i < word.length(); ++i) {
hash *= 33;
hash %= mod;
hash += word.charAt(i);
hash %= mod;
}
return hash;
}
一個散列函數的基本想法是創建一個散列從它的詞,然後填寫該數組用布爾真,則表示它的存在。
當您創建一個包含所有散列值的整個hashTable時。然後,只需爲目標單詞創建一個散列並檢查它是否存在於hashTable中。
這顯然是一項家庭作業。我確信這會在Java101中引起一片眉毛。 –
- 1. 檢查是否加載了.txt文件
- 2. 讀取.txt文件
- 3. 讀取.txt文件
- 4. 是否可以將PDF文件作爲txt文件讀取?
- 5. 檢查.txt文件的文件夾是否包含列表框中的單詞
- 6. 從.txt文件到詞典
- 7. C++無法讀取txt文件中的單詞
- 8. 從.txt文件中讀取和寫入.txt文件
- 9. 錯誤讀取TXT文件
- 10. C++讀取txt文件?
- 11. 讀取文件.txt C++
- 12. FStream讀取txt文件
- 13. 用javascript讀取.txt文件
- 14. 如何讀取txt文件。?
- 15. 讀取一個txt文件
- 16. 從txt讀取文件
- 17. 用C++讀取.txt文件
- 18. 用php讀取txt文件
- 19. Java /從.txt文件讀取
- 20. 按行讀取txt文件
- 21. 用Java讀取.txt文件?
- 22. 從txt文件讀取char是錯的
- 23. Obj C讀取文件,未找到txt文件
- 24. 通過包含單詞的txt文件的行讀取
- 25. JavaScript查找.TXT文件
- 26. 找到.txt文件中的所有單詞c#
- 27. 如何讀取.txt文件以長[] JAVA?
- 28. 如何從編號的txt文件讀取? (data1.txt,data2.txt等)
- 29. 查看.txt,.pdf文件中的iphone
- 30. FileReader/Scanner讀取文件名,而不是.txt文件的內容
提示:使用'Set list = new HashSet ();' –
3kings