只接受一个字母输入

时间:2013-12-07 06:01:24

标签: java error-handling

我正在处理我的hangman程序的错误处理。如果用户输入数字(int / double)而不是字母(char / string),我想打印出错误消息。我该怎么做?

这是引擎类的代码:

//hangman viewer stuff
//////////////////////////////////////////////////////////////
JFrame frame = new JFrame();

frame.setSize(200,375); //invoked the method setSize on the implicit parameter frame
frame.setTitle("Hangman"); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

HangmanComponent g = new HangmanComponent();
frame.add(g);

frame.setVisible(true);

///////////////////////////////////////////////////////////////

String wordd = JOptionPane.showInputDialog("Type in a word.");
int length = wordd.length();
String blank = "_ ";
String word2 = new String("");
int guesscount = 10;

ArrayList<String>answers=new ArrayList<String>(); //creates reference to empty structure that will contain references
char blanks[]=new char[wordd.length()]; //creates an array with the same number of terms as the length of the word
for (int i=0; i<length; i++)//fills the array with blanks corresponding to the length of the word
{
    blanks[i] = '_';
}

HangmanComponent y = new HangmanComponent();

while (true)
{
    String letter = JOptionPane.showInputDialog("Guess a letter! You have "+guesscount+" guesses."+"\n"+answers+"\n"+Arrays.toString(blanks).replace(",", " ").replace("[","").replace("]","")); //Prints a space
    char letterchar = letter.charAt(0); //converts string letter to char letterchar
    int idx = 0;
    boolean found = false;
    answers.add(letter); //adds the string to the arraylist answers

    while (idx >= 0 && idx < length) //idx is greater than or equal to 0 but less than the length of the word
    {
        //System.out.println("idx = " + idx);
        idx = wordd.indexOf(letter, idx); //idx is the index of "letter" in "wordd" and finds all instances of the letter
        //System.out.println("idx = " + idx + ", guesscount = " + guesscount);
        if (idx != -1) //if idx is not -1 (the letter exists in the word)
        {
            found = true;
            blanks[idx] = letterchar; //sets the term in the array equal to the letter 
            idx += 1; //idx=idx+1
        } 
        else 
        {
            guesscount=guesscount-1;
            y.nextStage();
            y.printStage();
            frame.add(y);
            frame.setVisible(true);
            break;
        }
    }

    if (found)
    {
        JOptionPane.showMessageDialog(null, Arrays.toString(blanks).replace(",", " ").replace("[","").replace("]","")+"\n"+"You found a letter!"+"\n"+answers);
    }
    else 
    {
        JOptionPane.showMessageDialog(null, Arrays.toString(blanks).replace(",", " ").replace("[","").replace("]","")+"\n"+"That letter is not in the word! Guess again!"+"\n"+answers);

        if (guesscount == 0)
        {
            JOptionPane.showMessageDialog(null, "Sorry, you're all out of guesses. The answer was '"+wordd+".' Thanks for playing!");
            break;
        }
    }

    char [] lettersArray = wordd.toCharArray(); //converts word to array of chars

    if (Arrays.equals(blanks, lettersArray))//compares array of blanks to array of letters
    {
        JOptionPane.showMessageDialog(null, "You guessed the word! Thanks for playing!");
        break;
    }
}

}

3 个答案:

答案 0 :(得分:3)

您也可以使用正则表达式:

  if(str.matches(".*\\d.*")){
    // contains a number
  } else{
   // does not contain a number
  }

答案 1 :(得分:3)

尝试使用正则表达式检查单词只包含字母。

 String wordd = JOptionPane.showInputDialog("Type in a word.");
 if(!wordd.matches("[a-zA-Z]+")){
   // Invalid word.
 }

答案 2 :(得分:1)

只需接受来自用户的输入&amp;然后

试试这个。

try{
    Integer.parseInt(yourInput);
     //error message
}catch(Exception e){
    //output
}