2014-03-03 66 views
-1

我製作了自己的單詞字典,並希望檢查用戶的輸入是否拼寫正確,方法是將其製作成數組並將其與我的字典數組進行比較。問題是我的結果表明我輸入的所有單詞拼寫錯誤。Java中的拼寫檢查器

import java.util.*; 

public class SpellCheck { 
    public static void main(String args[]){ 
     String array[] = {"This", "is", "a", "string"}; // Dictionary 
     System.out.println("Please enter a sentence"); 
     Scanner a = new Scanner(System.in); 
     String line = a.nextLine(); 
     System.out.println(line); 
     String arr[] = line.split(" "); // Turning into an array 
     for(int i = 0; i<array.length; i++){ // Loop that checks words 
      for(int j=0; j<arr.length; j++){ 
       if(array[i].equals(arr[j])){ 
        System.out.println(arr[j] + " is spelled correctly"); 
       } 
       else{ 
        System.out.println(arr[j] + " is not spelled correctly"); 
       } 
      } 
     } 
    } 
} 
+2

你應該解決你的第一個問題。有不好的問題擺在他們的身邊和副本。 – djechlin

+0

建議:編輯你的第一個問題,而不是創建新的。 –

+2

您使用什麼輸入? – Christian

回答

0

既然你用的arr,甚至是每個字(大部分時間)的array每個字比較,你會得到"... is not spelled correctly"。您可以嘗試通過arr(從輸入字)循環和檢查,如果array包含單詞:

List<String> list = Arrays.asList(array); 

for (int i = 0; i < arr.length; i++) { // loop through input 
    if (list.contains(arr[i])) { 
     System.out.println(arr[i] + " is spelled correctly"); 
    } else { 
     System.out.println(arr[i] + " is not spelled correctly"); 
    } 
} 

此:

List<String> list = Arrays.asList(array); 

將數組String[]轉換爲List<String>,並將其存儲到list,所以你可以在循環中使用它的方法contains(key)

注:

這將不處理的情況下詞語的降低和條件轉換爲大寫是不同的,要解決這個問題,你可以填寫array只有小寫單詞,然後創建一個新的數組(從arr)所有單詞轉換成小寫。

+0

這很好,謝謝。 – user3008456