Reputation: 57
i seem to have a problem which i cant fix, when a user enters the complete word for my program it displays each time it searches a char instead of just displaying the whole word saying that they had guessed it correctly. How would i get it to just display the word only and not show each time it search's a char when the user enters the whole word? thanks for future replys
package assignment1Q2;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HangmanClassExample {
static Scanner keyboard = new Scanner(System.in);
static int play, size, size2;
static String word;
static String[] ARRAY = new String[0];
public static void main(String[] args) {
setUpGame();
}
public static void setUpGame() {
System.err.printf("Welcome to hangman.\n");
try {
Scanner scFile = new Scanner(new File("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt"));
String line;
while (scFile.hasNext()) {
line = scFile.nextLine();
Scanner scLine = new Scanner(line);
size++;
}
ARRAY = new String[size];
Scanner scFile1 = new Scanner(new File("H:\\Varsity work\\Java Programming\\Programs\\HangMan\\src\\hangman\\HangMan.txt"));
while (scFile1.hasNext()) {
String word;
line = scFile1.nextLine();
Scanner scLine = new Scanner(line);
word = scLine.next();
ARRAY[size2] = word;
size2++;
calculateGuess();
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
public static void calculateGuess() {
try {
do {
int random = (int) (Math.random() * ARRAY.length);
String randomWord = ARRAY[random];
String word = randomWord;
char[] ranWord = randomWord.toCharArray();
char[] dash = word.toCharArray();
int LEFT = 6;
for (int i = 0; i < dash.length; i++) {
dash[i] = '-';
System.out.print(dash[i]);
}
for (int A = 1; A <= dash.length;) {
System.out.print("\nGuess a Letter:");
String userletters = keyboard.next();;
for (int i = 0; i < userletters.length(); i++) {
char userLetter = userletters.charAt(i);
String T = Character.toString(userLetter);
for (int B = 0; B < ranWord.length; B++) {
if (userLetter == dash[B]) {
System.out.println("this '" + userLetter + "' letter already exist");
B++;
if (userLetter == dash[B-1]) {
break;
}
} else if (userLetter == ranWord[B]) {
dash[B] = userLetter;
A--;
}
}
if (!(new String(ranWord).contains(T))) {
LEFT--;
System.out.println("You did not guess a correct letter, you have " + LEFT + " OF "
+ dash.length + " trys left to guess correctly");
}
System.out.println(dash);
}
if ((new String(word)).equals(new String(dash))) {
System.out.println("\nYou have guessed the word correctly!");
break;
}
}
System.out.println("Play agian? (y/n)");
String name = keyboard.next();
if (name.equals("y")) {
play = 0;
} else {
play = 1;
return;
}
} while (play == 0);
} catch (NullPointerException e) {
}
}
}
OUTPUT:
Welcome to hangman.
--------
Guess a Letter:c
c-------
Guess a Letter:c
this 'c' letter already exist
c-------
Guess a Letter:computer
this 'c' letter already exist
c-------
co------
com-----
comp----
compu---
comput--
compute-
computer
You have guessed the word correctly!
Play agian? (y/n)
n
Upvotes: 0
Views: 81
Reputation: 101
You could take the if out of your for loop like this
if ((new String(word)).equals(new String(dash))) {
System.out.println("\nYou have guessed the word correctly!");
// break; not needed anymore as we're out of the loop
}
else {
for (int i = 0; i < userletters.length(); i++) {
char userLetter = userletters.charAt(i);
String T = Character.toString(userLetter);
for (int B = 0; B < ranWord.length; B++) {
if (userLetter == dash[B]) {
System.out.println("this '" + userLetter + "' letter already exist");
B++;
if (userLetter == dash[B-1]) {
break;
}
} else if (userLetter == ranWord[B]) {
dash[B] = userLetter;
A--;
}
}
if (!(new String(ranWord).contains(T))) {
LEFT--;
System.out.println("You did not guess a correct letter, you have " + LEFT + " OF "
+ dash.length + " trys left to guess correctly");
}
System.out.println(dash);
}
}
}
But in my opinion, in that kind of game, the user should only be able to input one character and not the whole word.
Upvotes: 0
Reputation: 3927
Instead of having two for loops and checking each character, you can use startsWith
to check for character that user has entered. For e.g. if user enters, comp
, you can simply check originalString.startsWith(comp)
- if true, just print comp and remove the first 4 characters from originalString.
Upvotes: 1