2015-01-16 19 views
0

我想從一組拼寫字母中獲取有效的單詞,但是所有輸入(包含無效單詞,包括數字)都被我的代碼接受,並根據列表進行檢查的話直接。我怎樣才能檢查輸入與顯示的拼寫錯誤的字母,以便在檢查記錄之前只接受包含拼寫錯誤字母的單詞?如何在接受各種字符串之前進行代碼檢查輸入

import comp102x.IO; //a library from an edx course(COMP 102x) 
import java.io.File; 
import java.io.IOException; 
import java.util.Scanner; 
import javax.swing.JOptionPane; 
import java.util.Random; 

public class FormWords { 

public void searchWord() throws IOException 

{ 
      Random random = new Random(); 
      String [] randAlphs = {"o","b","e","k","a","i","c","d","f","g","h","k","u"}; 
      int r = random.nextInt(randAlphs.length); 
      int a = random.nextInt(randAlphs.length); 
      int n = random.nextInt(randAlphs.length); 
      int d = random.nextInt(randAlphs.length); 
      int o = random.nextInt(randAlphs.length); 
      int m = random.nextInt(randAlphs.length); 
      int w = random.nextInt(randAlphs.length); 
      int i = random.nextInt(randAlphs.length); 
      int s = random.nextInt(randAlphs.length); 

//prompt's user for input 

String answer = JOptionPane.showInputDialog("form words with the following: " + randAlphs[r]+ randAlphs[a]+ randAlphs[n]+ randAlphs[d]+ randAlphs[o]+ randAlphs[m]+ randAlphs[w]+ randAlphs[s]); 

/*searches the record to check if input exists 
*/ 
boolean exist = searchFromRecord("record.txt", answer); 

if (exist) 
{ 

    JOptionPane.showMessageDialog(null, "Congratulation!The word \"" + answer + "\" is a valid English word."); 
} 
else 
{           
    // System.out.println("Sorry b ut the word \"" + answer + "\" is not a valid English word."); 
    JOptionPane.showMessageDialog(null, "Sorry but the word \"" + answer + "\" is not a valid English word."); 
} 
} 



/** 
* Searches the record and returns if a specified word is in the record. 
* 
* @param recordName The name of the record text file. 
* @param word The word to be searched. 
* @return true if the word presents in the record, false otherwise. 
*/ 

private boolean searchFromRecord(String recordName, String word) throws IOException 
{ 
      // Please write your code after this line 
      File inputFile = new File("record.txt"); 
      Scanner input = new Scanner(inputFile); 
      while(input.hasNextLine()){ 
      if(word.equalsIgnoreCase(input.nextLine())){ 
       return true; 
       } 

      } 
      return false; 

    } 


}   

回答

0

在您的「提示用戶輸入」代碼之前。你可以在java中使用HashSet並把所有的字母加入到de HashSet中。代碼片斷如下:

Set<String> scrabbledLettersSet = new HashSet<String>(); 
scrabbledLettersSet.add(randAlphs[a]); 
scrabbledLettersSet.add(randAlphs[n]); 
scrabbledLettersSet.add(randAlphs[d]); 
scrabbledLettersSet.add(randAlphs[o]); 
scrabbledLettersSet.add(randAlphs[m]); 
scrabbledLettersSet.add(randAlphs[w]); 
scrabbledLettersSet.add(randAlphs[i]); 
scrabbledLettersSet.add(randAlphs[s]); 

然後你做searchFromRecord方法之前,您可以使用set來檢查你的輸入是否有效。代碼片段是這樣的:

bool containsScrabbledLetters = false; 
for(int i = 0 ; i < answer.length() ; ++ i) { 
    if (scrabbledLettersSet.contains(answer.substring(i, i + 1))) { 
     containsScrabbledLetters = true; 
     break; 
    } 
} 

boolean exist = false; 
if (containsScrabbledLetters) 
    exist = searchFromRecord("record.txt", answer); 

上面的代碼意味着如果在輸入字符串中的至少一個字母是亂畫字母,輸入就OK了,如果你是指所有字母詮釋了輸入的字符串必須在摸索中存在信,你可以使用下面的代碼:

bool containsScrabbledLetters = true; 
for(int i = 0 ; i < answer.length() ; ++ i) { 
    if (!scrabbledLettersSet.contains(answer.substring(i, i + 1))) { 
     containsScrabbledLetters = false; 
     break; 
    } 
} 

boolean exist = false; 
if (containsScrabbledLetters) 
    exist = searchFromRecord("record.txt", answer); 
+0

@Jianguan ...好,謝謝,我會檢查出來,並送還給你 – Sam

+0

你已經真正的幫助...謝謝! @Jianguan – Sam

相關問題