Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 只接受字母输入_Java_Error Handling - Fatal编程技术网

Java 只接受字母输入

Java 只接受字母输入,java,error-handling,Java,Error Handling,我正在处理我的刽子手程序的错误。如果用户输入的是数字(int/double)而不是字母(char/string),我想打印一条错误消息。我该怎么做 这是发动机等级的代码: //hangman viewer stuff ////////////////////////////////////////////////////////////// JFrame frame = new JFrame(); frame.setSize(200,375); //invoked the method set

我正在处理我的刽子手程序的错误。如果用户输入的是数字(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;
    }
}
//刽子手查看器的东西
//////////////////////////////////////////////////////////////
JFrame=新JFrame();
框架。设置尺寸(200375)//在隐式参数框架上调用方法setSize
帧。设置标题(“刽子手”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
HangmanComponent g=新的HangmanComponent();
框架。添加(g);
frame.setVisible(true);
///////////////////////////////////////////////////////////////
String wordd=JOptionPane.showInputDialog(“键入一个单词”);
int length=wordd.length();
字符串blank=“\”;
String word2=新字符串(“”);
int猜测计数=10;
ArrayListSwers=新的ArrayList()//创建对将包含引用的空结构的引用
字符空白[]=新字符[wordd.length()]//创建与单词长度具有相同字数的数组
for(int i=0;i=0&&idx

}只需接受用户的输入,然后

试试这个

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

只需接受用户的输入&然后

试试这个

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

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

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

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

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

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

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

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

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

出于好奇,这一切都写在主函数中了吗?您可以创建一个可接受字符的数组,并创建一个方法对其进行排序,检查变量(无论输入是什么)是否等于其中任何一个;如果没有,抛出一个错误并停止程序,或者再次要求他们输入正确的字符(假设使用while循环)。这就是我要做的,尽管我只是用Ruby做的,从来没有用Java做过。自动检查可能有很多更好的方法,但我不知道。@TheGreenColla是的,这都在主要函数中。有人为我的Java初学者类完成了这个项目,你应该真正学习面向对象编程。。它让生活变得简单多了。一些“A3”允许吗?出于好奇,这都是写在主函数中的吗?你可以创建一个可接受的字符数组,并创建一个方法对其进行排序,检查变量(无论输入是什么)是否等于其中任何一个;如果没有,抛出一个错误并停止程序,或者再次要求他们输入正确的字符(假设使用while循环)。这就是我要做的,尽管我只是用Ruby做的,从来没有用Java做过。自动检查可能有很多更好的方法,但我不知道。@TheGreenColla是的,这都在主要函数中。有人为我的Java初学者类完成了这个项目,你应该真正学习面向对象编程。。它让生活变得简单多了。一些“A3”允许吗?事实上,错误消息应该在try中,而不是catch中。这是因为op希望如果它是一个数字,它会显示错误消息。实际上,错误消息应该在try中,而不是catch中。这是因为op希望它显示错误消息(如果是数字)。