Hangman (Infinite Loop Problem)

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 2600 Views - Last Post: 12 January 2014 - 05:55 PM Rate Topic: -----

#16 bluejquestioner1213   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 29-December 13

Re: Hangman (Infinite Loop Problem)

Posted 07 January 2014 - 07:34 PM

View Postmacosxnerd101, on 07 January 2014 - 07:32 PM, said:

Why are you making a super() call and passing those parameters? Buffin does not extend anything explicitly. So you have to satisfy the Object super constructor by invoking super();. The Object super constructor does not accept any parameters.

So that means I should just get rid of that line totally since it does not extend any object?
Was This Post Helpful? 0
  • +
  • -

#17 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Hangman (Infinite Loop Problem)

Posted 07 January 2014 - 07:37 PM

Give a try debugging based on my advice. You should be able to figure it out based on what I said and some trial and error.
Was This Post Helpful? 0
  • +
  • -

#18 bluejquestioner1213   User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 43
  • Joined: 29-December 13

Re: Hangman (Infinite Loop Problem)

Posted 12 January 2014 - 05:41 PM

Ok. I figured that out, but I have more issues. WIth my hangman class, it states that there is no suitable constructor for the scanner, so how would I make a constructor specifically for the scanner part my code?
 Scanner scanner = new Scanner(); 
Here is the entire code:


import javax.swing.JOptionPane;
import java.util.Scanner; 
import java.util.Random;
public class Hangman extends BasicGame
{
    private String SecretWord;
    private String Letter;
    private String DisplayWord;
    private String[ ] words;
    private int errors;
    private int correct;
    private static java.util.Random randy = new java.util.Random();
    public Hangman()
    {
         super();
         words = new String[50];
         Scanner scanner = new Scanner();
         for (int j = 0; j < 50; j++) 
         {          
             String i = scanner.next();
             words[j] = i;
         }
         int a = randy.nextInt(50);
         SecretWord = words[a];
         DisplayWord="";
         Letter = "";
    }
    public void askUsersFirstChoice()
    {    
        String a = ("");
        JOptionPane.showMessageDialog(null,SecretWord);
        while(a.equals(""))
        {
            a = JOptionPane.showInputDialog("Guess a letter: ");
            if (a != null && ! a.equals (""))
                Letter = a;
            else
            {
                a = JOptionPane.showInputDialog("Guess a valid letter: ");
            }
        }
        JOptionPane.showMessageDialog(null,Letter.charAt(0));
        if(errors<6 || shouldContinue() == true)
        {
            showUpdatedStatus();
            askUsersNextChoice();   
        }
        else
        {
            showFinalStatus();
        }
    }    //=======================
    public void askUsersNextChoice()
    {   
        JOptionPane.showInputDialog("Guess another letter in the word: ");
    }  //======================
     public boolean shouldContinue()  
     {    
         if(DisplayWord.equals(SecretWord))
         {
             return true;
            }
         return false;
     }  //======================

    public void showUpdatedStatus()
    {  
        if(SecretWord.contains(Letter))
        {
            for(int i = 0; i< 5; i++)
            {
                if(SecretWord.charAt(i) == Letter.charAt(0))
                DisplayWord.concat(Letter);
                else
                DisplayWord.concat("_");
            }
            JOptionPane.showMessageDialog(null,"it's in the word");
            JOptionPane.showMessageDialog(null,DisplayWord);
        }
        else
        {
            errors ++;
            if(correct==0)
            {
                errors++;
                JOptionPane.showMessageDialog(null,"That letter isn't in the word.  You have " + (6-errors) + " mistakes left");
            }
        }
    }
    public void showFinalStatus()  
    {    
        if(errors <6)
        {
            JOptionPane.showMessageDialog(null,"You win!");
        }
        else
        {
            JOptionPane.showMessageDialog (null,
            "You lose! The word was: " + SecretWord);
            
        }
    }  //======================
}


Was This Post Helpful? 0
  • +
  • -

#19 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: Hangman (Infinite Loop Problem)

Posted 12 January 2014 - 05:55 PM

You had the Scanner working, it seems, on your code from the first page. Look at that.

Note: If you want to read in from the Console instead, you need to pass System.in to the Scanner constructor.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2