Note that there are some explanatory texts on larger screens.

plurals
  1. POProgramming Hangman in Java, exception in main thread error (along with others)
    primarykey
    data
    text
    <p>So I have this program somewhat functional so far. I've been using switch statements. I'm not sure if that's proper logic for this, but I have it working. When I input the word I want, it works, and starts drawing the hangman after the first input, but then runs into an error.</p> <pre><code> public class Hangman { public static void main (String[] args){ JFrame frame = new JFrame ("Hangman"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); HangmanPanel Panel = new HangmanPanel(); frame.getContentPane().add(Panel); frame.pack(); frame.setVisible(true); } } </code></pre> <p>here's my other class</p> <pre><code> import javax.swing.JPanel; import java.awt.event.*; import javax.swing.*; import java.util.Random; import java.util.Scanner; import java.io.*; import java.util.Scanner; public class HangmanPanel extends JPanel { int lives; String guessletter; String inputw; char[] hiddenWord; char[] aOfWord; String[] words = { "ADA", "COBOL", "LOGO", "BASIC", "PROLOG", "UBUNTU", "UHURU" }; Scanner scan = new Scanner(System.in); boolean isFound; int a; public HangmanPanel() { System.out.println("Enter the word to be searched: "); inputw = scan.nextLine(); aOfWord = inputw.toCharArray(); this.setLives(inputw.length()); // this.output(); hiddenWord = new char[aOfWord.length]; for (int j = 0; j &lt; hiddenWord.length; j++) hiddenWord[j] = '*'; this.output(); while (lives &gt; 0) System.out.println("Please choose a letter: "); guessletter = scan.nextLine(); this.checkForMatch(guessletter); if (isFound == true) { hiddenWord[a] = guessletter.charAt(0); } else { System.out.println("_____"); System.out.println("| |"); System.out.println("| "); System.out.println("| "); System.out.println("| "); this.reduceLives(); while (lives &gt; 0) System.out.println("Please choose a letter: "); guessletter = scan.nextLine(); this.checkForMatch(guessletter); if (isFound == true) { hiddenWord[a] = guessletter.charAt(1); } else { System.out.println("_____"); System.out.println("| |"); System.out.println("| o"); System.out.println("| "); System.out.println("| "); this.reduceLives(); } while (lives &gt; 0) System.out.println("Please choose a letter: "); guessletter = scan.nextLine(); this.checkForMatch(guessletter); if (isFound == true) { hiddenWord[a] = guessletter.charAt(2); } else { System.out.println("_____"); System.out.println("| |"); System.out.println("| o"); System.out.println("| | "); System.out.println("| "); this.reduceLives(); } while (lives &gt; 0) System.out.println("Please choose a letter: "); guessletter = scan.nextLine(); this.checkForMatch(guessletter); if (isFound == true) { hiddenWord[a] = guessletter.charAt(3); } else { System.out.println("_____"); System.out.println("| |"); System.out.println("| o"); System.out.println("| /| "); System.out.println("| "); this.reduceLives(); } while (lives &gt; 0) System.out.println("Please choose a letter: "); guessletter = scan.nextLine(); this.checkForMatch(guessletter); if (isFound == true) { hiddenWord[a] = guessletter.charAt(4); } else { System.out.println("_____"); System.out.println("| |"); System.out.println("| o"); System.out.println("| /|\\ "); System.out.println("| "); this.reduceLives(); } while (lives &gt; 0) System.out.println("Please choose a letter: "); guessletter = scan.nextLine(); this.checkForMatch(guessletter); if (isFound == true) { hiddenWord[a] = guessletter.charAt(5); } else { System.out.println("_____"); System.out.println("| |"); System.out.println("| o"); System.out.println("| /|\\ "); System.out.println("| \\"); this.reduceLives(); } while (lives &gt; 0) System.out.println("Please choose a letter: "); guessletter = scan.nextLine(); this.checkForMatch(guessletter); if (isFound == true) { hiddenWord[a] = guessletter.charAt(6); } else { System.out.println("_____"); System.out.println("| |"); System.out.println("| o"); System.out.println("| /|\\"); System.out.println("| / \\"); this.reduceLives(); } } this.output(); } public void setLives(int a) { this.lives = a; } public void reduceLives() { lives = lives - 1; System.out.println("Lives remaining: " + this.getLives()); } public int getLives() { return lives; } public void output() { System.out.println("Lives remaining: " + this.getLives()); System.out.println("Word found so far "); for (int i = 0; i &lt; hiddenWord.length; i++) { System.out.print(hiddenWord[i] + "\n"); } } public void checkForMatch(String l) { for (int i = 0; i &lt; aOfWord.length; i++) { if (l.charAt(0) == aOfWord[i]) { isFound = true; a = i; break; } else { isFound = false; } } } } </code></pre>
    singulars
    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