Search code examples
stringvalidationrecord

how do i make code check inputs before accepting all kinds of strings


I am trying to get a valid word from a set of scrabbled letters but all inputs (invalid ones,numbers included) are accepted by my code and checked against a list of words directly. How can i check the inputs against the displayed scrabbled letters so that only words containing letters from the scrabbled ones are accepted before checking the record?

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;

    }


}         

Solution

  • Before your "prompt's user for input" code. You can use and HashSet in java and add all the letter into de HashSet. Code snippet like this:

    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]);
    

    Then before you do searchFromRecord method, you can use the set to check whether you input is valid or not. Code snippet is like this:

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

    The above code means that if at least one letter in input string is the scrabbled letter, the input is OK, If you means all letters int the input string must exist in the scrabbled letters, you can use the following code:

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