Hey guys i have a weird problem but its probably something small. i have a hang man program that has no errors. i am using netbeans to compile my project but when i run it it nothing come up or prints out. i don't know what im doing wrong because i have no errors:/ heres my code
Hangman class (main)
package ****;
import static java.lang.System.out;
import java.util.Scanner;
public class Hangman {
static final boolean DEBUG = true;
static char promptAndGetResponse(Scanner sc, String prompt) {
prompt = "Please guess a letter---> ";
out.println("Welcome to Hangman ...");
out.println("\t... a program which allows you to play a hame of hanhman .");
out.print(prompt);
sc = new Scanner(System.in);
String line= sc.nextLine();
line.toLowerCase();
return line.charAt(0);
}
/**
* Play out the guessing of one word.
*/
static void play1round(Scanner sc) {
Word word = null;
Word copy = null;
Word foundSoFar = null;
char guess;
word = new Word();
copy = new Word(word);
word = new Word('-', word.length());
if (DEBUG) {
out.println("The word is " + word);
out.println();
}
while (true) {
out.println("So far: " + foundSoFar);
guess = promptAndGetResponse(sc, "\nEnter guess (0 to exit) > ");
if (guess == '0') {
break;
}
if (DEBUG) {
out.println("Your guess was " + guess);
}
// note: matched adds matched letter to "foundSoFar"
// and changes the matched letter in "copy" to '-'
if (Word.matched(guess, foundSoFar, copy)) {
// FOR STUDENT TO COMPLETE
if (word.equals(foundSoFar)) {
out.println("Thats Right !!!!");
break;
}
}
}
}
/**
* main entry point.
* @return neglects to set any exit codes
* @param args name of data file
*/
public static void main(String[] args) throws java.io.FileNotFoundException {
java.io.File file = new java.io.File("words.txt");
if (!file.canRead()) {
out.println("Can't read file words.txt");
System.exit(1);
}
java.io.FileInputStream fis = new java.io.FileInputStream(file);
java.util.Scanner sc = new java.util.Scanner(fis);
Word.initializeWordlist(sc);
sc.close();
sc = new java.util.Scanner(System.in);
char c = promptAndGetResponse(sc, "Want to guess a word? ");
while (c == 'y') {
play1round(sc);
c = promptAndGetResponse(sc, "Want to guess a word? ");
}
}
}
And this is my word class to get the words
package *****;
import static java.lang.System.out;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Random;
public class Word {
private static java.util.ArrayList<String> wordlist = null;
private char[] lexeme = null; // the characters in the word
/**
* Build a list of words from file.
* @param sc - connected to file containing word list
* @global wordlist - this method adds content to wordlist
*/
@SuppressWarnings("empty-statement")
public static void initializeWordlist(Scanner sc) {
wordlist = new java.util.ArrayList<String>();
int i = 0;
while (sc.hasNext());
String word = sc.next();
wordlist.add(word);
i++;
}
/**
* Construct a random Word object.
* @global wordlist - copies a word from here
*/
public Word() {
Random random = new Random();
int randomWord = random.nextInt(wordlist.size());
String word = wordlist.get(randomWord);
lexeme = new char[word.length()];
for (int i = 0; i < lexeme.length; i++) {
lexeme[i] = word.charAt(i);
}
}
/**
* Construct a copy of a give Word object.
* @param word - word to copy
*/
public Word(Word word) {
lexeme = new char[word.length()];
for (int i = 0; i < this.lexeme.length; i++) {
this.lexeme[i] = word.lexeme[i];
}
}
/**
* Construct a word of a given length repeating a single character.
* @param ch - char to repeat
* @param length - number of times to repeat ch
*/
public Word(char ch, int length) {
lexeme = new char[length];
for (int j = 0; j < lexeme.length; j++) {
lexeme[j] = ch;
}
}
public int length() {
return lexeme.length;
}
public String toString() {
return new String(lexeme);
}
/**
* Search for a matched character.
* @return: whether "copy" contains the char denoted by "guess"
* @param guess the user's attempt at guessing a letter
* @param disposableCopy a copy of secret word which we can destroy
* @param whatWeHaveSoFar the part of the word we've solved so far
*/
public static boolean matched(char guess,
Word whatWeHaveSoFar,
Word disposableCopy) {
for (int i = 0; i < disposableCopy.lexeme.length; i++) {
if (guess == disposableCopy.lexeme[i]) {
whatWeHaveSoFar.lexeme[i] = guess;
disposableCopy.lexeme[i] = '-';
return true;
}
}
return false;
}
/**
* Does this Word object equal another Word object?
* @return whether or not the contents in the two lexemes equal?
*/
public boolean equals(Word otherWord) {
if (this.lexeme.length != otherWord.lexeme.length) {
return false;
}
for (int i = 0; i < this.lexeme.length; i++) {
if (this.lexeme[i] != otherWord.lexeme[i]) {
return false;
}
}
return true;
}
}