Java Hangman program! Need help reading an ar
This program runs but does not execute this part of the code how I want it to. When I take out what is posted below the program works fine but when I leave it in the program ends after the first letter is entered..
I want the array guesshold to hold the guesses from the user and in this part i have posted below compare the input with everything else that has been entered to see if it has been previously entered. if you want to give any other input on other parts please feel free i am still a beginner!
/*
for(int counter = 0; counter <= 10; counter++)
{
if(guesshold[counter +1 ].equals(guess1))
{
System.out.println("You have already guessed that letter");
}
*/
Code :
import java.lang.*;
import java.util.*;
import java.io.*;
public class Program
{
/**
* This is the main entry point for the application
*/
static Scanner console = new Scanner(System.in);
public static void main(String args[])
{
System.out.println("Welcome to Hangman!");
System.out.println();
System.out.println("The word is: _ _ _ _ "); // the word is cave
String correctguess = "_ _ _ _";
String cave = "cave";
boolean z = false;
String replace1 = "1234";
int i = 0;
do
{
int x = 0;
int y = 0;
System.out.println("Please enter your one letter guess");
String guess1 = console.next();
guess1 = guess1.toLowerCase();
String guesshold[] = new String[11];
guesshold[x] = guess1;
if(guess1.equals("c"))
{
System.out.println("Nice " +guess1+ " is a correct letter!");
replace1 = replace1.replace('1' , 'c');
}
else if(guess1.equals("a"))
{
System.out.println("Nice " +guess1+ " is a correct letter!");
replace1 = replace1.replace('2' , 'a');
}
else if(guess1.equals("v"))
{
System.out.println("Nice " +guess1+ " is a correct letter!");
replace1 = replace1.replace('3' , 'v');
}
else if(guess1.equals("e"))
{
System.out.println("Nice " +guess1+ " is a correct letter!");
replace1 = replace1.replace('4' , 'e');
}
else
{
String wrong1 = "XXXXXXX";
wrong1 = wrong1.substring(0,i);
System.out.println("Wrong X" +wrong1);
int wronglength = wrong1.length();
i++;
if(wronglength == 7)
{
z = true;
}
}
if(replace1.equals(cave))
{
z = true;
System.out.println("You WON the guessed the correct letters in the word CAVE");
}
for(int counter = 0; counter <= 10; counter++)
{
if(guesshold[counter +1 ].equals(guess1))
{
System.out.println("You have already guessed that letter");
}
}
}while(z == false);
}//ends main method
}//ends class
Re: Java Hangman program! Need help reading an ar
Quote:
the program ends after the first letter is entered..
Does it end with an error? If so, please copy the full text and paste it here.
Otherwise what happens when? Can you copy the full contents of the console and paste it here that shows what the program prints out?
On windows: To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
Re: Java Hangman program! Need help reading an ar
no the program runs but logically it does not work. when i run it and enter a letter the program just ends itself..
Re: Java Hangman program! Need help reading an ar
Quote:
the program just ends itself..
Without any errors?
Try debugging the code by adding some println statements to show where the execution goes and to show the values of variables as they are used and changed. If you add enough println statements you'll know where the execution stopped by seeing which println did not print which will tell you that the program stopped executing before it got to that println.
Copy the console's contents and paste it here when done testing if you have questions about what the code is doing as it executes.
Re: Java Hangman program! Need help reading an ar
Quote:
Originally Posted by
mmagyar14
no the program runs but logically it does not work. when i run it and enter a letter the program just ends itself..
Not for me. I get a "java.lang.NullPointerException". This occurs at the code you mention in your first post -> either after a successful or unsuccessful guess is made.
My advice would be to improve your coding style by picking better variable names. Calling the code 'Program' is also not wise.
(maybe call this 'secretWord' or something equally more appropriate)
Your code also needs to be more generic to handle different words.