我如何在接受各种字符串之前进行代码检查输入
how do i make code check inputs before accepting all kinds of strings
我试图从一组乱七八糟的字母中获取一个有效的单词,但我的代码接受了所有输入(无效的,包括数字)并直接对照单词列表进行检查。我如何根据显示的拼写字母检查输入,以便在检查记录之前只接受包含拼写字母的单词?
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;
}
}
在您的 "prompt's user for input" 代码之前。你可以在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);
我试图从一组乱七八糟的字母中获取一个有效的单词,但我的代码接受了所有输入(无效的,包括数字)并直接对照单词列表进行检查。我如何根据显示的拼写字母检查输入,以便在检查记录之前只接受包含拼写字母的单词?
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;
}
}
在您的 "prompt's user for input" 代码之前。你可以在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);