2014-03-03 4 views
-1

필자는 내 자신의 단어 사전을 만들었고 배열로 만들고 내 사전 배열과 비교하여 사용자 입력의 철자가 올바른지 확인하려고합니다. 문제는 내 결과에 입력 한 단어의 철자가 잘못되었다는 것입니다.자바의 맞춤법 검사기

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

조언 : 첫 번째 질문을 편집하여 newone을 작성하십시오. –

+2

어떤 입력을 사용하고 있습니까? – Christian

답변

0

가끔 arr의 각 단어 (대부분의 시간)과 array의 각 단어를 비교하고 있기 때문에 당신이 "... is not spelled correctly"을 얻을 것이다. ,

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

List<String>에 배열 String[]을 변환하고, list로 저장합니다 :

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"); 
    } 
} 

이 : 당신은 array 단어가 포함 된 경우 arr (입력의 단어)를 통해 루핑 및 검사를 시도 할 수 있습니다 그래서 당신은 그 방법으로 contains(key)을 사용할 수 있습니다.

참고 : 단어가이 문제를 해결하기 위해 당신은 단지 소문자 단어 array를 입력 한 다음 새 배열을 만들 수있는, 낮은 및 uppercases의 측면에서 다른 경우이 사건을 처리하지 않습니다

(from arr) 모든 단어는 소문자로 변환됩니다.

+0

이것은 완벽하게 고마워했습니다. – user3008456