Note that there are some explanatory texts on larger screens.

plurals
  1. POAccept only a letter input
    primarykey
    data
    text
    <p>I'm working on error handling my hangman program. I want to print out an error message if the user inputs a number (int/double) instead of a letter (char/string). How do I do that?</p> <p>This is the code for the engine class:</p> <pre><code>//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&lt;String&gt;answers=new ArrayList&lt;String&gt;(); //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&lt;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 &gt;= 0 &amp;&amp; idx &lt; 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; } } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload