I have the majority of my program finished, but now that I have most of the code it is tough to find the errors. I have multiple errors at the moment, but the main error I really need help with is that my program will loop the same guess over & over if it is correct. It is in an infinite loop, & I cannot find where it is. This has also brought to my attention that my program will go into negative guesses as it is supposed to end when it gets to 0. Some other errors that would be nice to get help with is 1) it shows a correct guess as an incorrect guess 2) it can only replace one letter in the secret word if there are multiple it will give me an error & end the program. & 3) if I enter 9 to quit, it does not quit.
Thanks in advance for any help. I can add code if needed ( I am only posting the main body ATM.)
public static final int DICTIONARY = 15000;
public static final int GUESSES = 8;
public static final int SECRETLENGTH = 20;
public static void main(String[] args) {
int usedSize = 0, randomWord, guesses = GUESSES;
String word, secretWord, guess, incorrectGuess, correctWord, playAgain;
char letter;
try
{
// Set up connection to the input file
Scanner hangmanDictionary = new Scanner(new FileReader("dictionary.txt"));
String [] dictionary = new String [DICTIONARY];
while (usedSize < DICTIONARY && hangmanDictionary.hasNextLine()) {
dictionary[usedSize] = hangmanDictionary.nextLine();
usedSize++;
}
kbd.nextLine();
clearScreen();
randomWord = pickRandom(DICTIONARY);
word = dictionary[randomWord];
secretWord = secret(word);
//comment out when done testing
System.out.println(word);
System.out.println("Here is the word to guess: " + secretWord);
System.out.println("Enter a letter to guess, or 9 to quit.");
guess = kbd.next();
do {
while (!guess.equals("9") || !(guess.equals(word) && guesses > 0)) {
letter = guess.charAt(0);
incorrectGuess = "";
incorrectGuess += letter;
if (word.indexOf(letter) < 0) {
guesses--;
System.out.println("Incorrect guesses: " + incorrectGuess);
System.out.println("Number of guesses left: " + guesses);
System.out.println("Enter a letter to guess, or 9 to quit.");
guess = kbd.next();
}
else {
//FINSH THIS
correctWord = correctWord(guess, word, secretWord, letter);
System.out.println(correctWord);
System.out.println("Incorrect guesses: " + incorrectGuess);
System.out.println("Number of guesses left: " + guesses);
System.out.println("Enter a letter to guess, or 9 to quit.");
guesses--;
}
}
if (guess.equals("9")) {
System.out.println("Thanks for playing!");
System.exit(0);
}
if (guess.equals(word)) {
System.out.println("You won!");
}
if (guesses == 0) {
System.out.println("You are out of guesses.");
}
System.out.println("Play again? Y/N");
playAgain = kbd.nextLine().toUpperCase();
} while (playAgain.equals("Y"));
}
catch (FileNotFoundException e) {
System.out.println("There was an error opening one of the files.");
}
}
Here's my guess:
Did you forget to put guess = kbd.next(); if the user guessed a correct character?
The inner while loop is your main problem, i.e. think about what happens when you enter a valid letter (guess), in that case the first condition of the while loop OR condition is TRUE (assuming you don't have a 9 in your secret word), so the while loop is entered without entering the second part of the OR condition. After that you enter the else part of the IF statement (since it's a valid guess) but in the else part you're not asking for the next guess, so it returns to the start of the while loop with the same guess and hence infinite loop.
Similarly, if you enter 9 to exit !guess.equals("9") evaluates to FALSE, so the second part of the OR condition is entered, in the second part
!(guess.equals(word) && guesses > 0) evaluates to TRUE (unless the secret word contains a 9) so you enter the WHILE loop which is invalid. etc ...
Try to write small parts of the code using known parameters and then bring it all together, that way it'll be easier to construct and follow the logic.
Related
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
System.out.println("\nThe sum of the numbers is: " + getSumOfInput());
}
public static int getSumOfInput () {
int counter = 0;
int sumOfNums = 0;
Scanner userInput = new Scanner(System.in);
while(counter <= 10) {
System.out.print("Enter the number " + counter + ": ");
boolean checkValidity = userInput.hasNextInt();
if(checkValidity) {
int userNum = userInput.nextInt();
userInput.nextLine();
System.out.println("Number " + userNum + " added to the total sum.");
sumOfNums += userNum;
counter++;
} else {
System.out.println("Invalid input. Please, enter a number.");
}
}
userInput.close();
return sumOfNums;
}
}
Hello everybody!
I just started java and I learned about control flow and now I moved on to user input, so I don't know much. The problem is this code. Works just fine if you enter valid input as I tested, nothing to get worried about. The problem is that I want to check for wrong input from user, for example when they enter a string like "asdew". I want to display the error from else statement and to move on back to asking the user for another input, but after such an input the program will enter in an infinite loop displaying "Enter the number X: Invalid input. Please, enter a number.".
Can you tell me what's wrong? Please, mind the fact that I have few notions when it comes to what java can offer, so your range of solutions it's a little bit limited.
Call userInput.nextLine(); just after while:
...
while(counter <= 10) {
System.out.print("Enter the number " + counter + ": ");
userInput.nextLine();
...
The issue is, that once you enter intput, which can not be interpreted as an int, userInput.hasNextInt() will return false (as expected). But this call will not clear the input, so for every loop iteration the condition doesn't change. So you get an infinite loop.
From Scanner#hasNextInt():
Returns true if the next token in this scanner's input can be interpreted as an int value in the default radix using the nextInt() method. The scanner does not advance past any input.
The fix is to clear the input if you came across invalid input. For example:
} else {
System.out.println("Invalid input. Please, enter a number.");
userInput.nextLine();
}
Another approach you could take, which requires less input reads from the scanner, is to always take the next line regardless and then handle the incorrect input while parsing.
public static int getSumOfInput() {
int counter = 0;
int sumOfNums = 0;
Scanner userInput = new Scanner(System.in);
while (counter <= 10) {
System.out.print("Enter the number " + counter + ": ");
String input = userInput.nextLine();
try {
int convertedInput = Integer.parseInt(input);
System.out.println("Number " + convertedInput + " added to the total sum.");
sumOfNums += convertedInput;
counter++;
} catch (NumberFormatException e) {
System.out.println("Invalid input. Please, enter a number.");
}
}
return sumOfNums;
}
first off, the secret word is printed out as dashes, then the user puts in what letter they want to guess. if they guess the letter correctly then it will update the dashes. so if the word is java, it will show as ---- and if the user types a, then it will update and show -a-a . my program does that but it also adds extra dashes at the end and i don't know how to make it not print those extra dashes. and that brings me to another problem i am having, the user is asked at what indexes they want to guess the letter. so if the user types the letter a and at index 1, then the updated word will show -a--, but my program updates all instances of where the a is at, so it shows -a-a. here is my code:
import java.util.Scanner;
public class HangMan2 {
private static final boolean testingMode = true;
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int guessRemaining = 20;
int rounds = 1;
int roundScore;
String wordString = "";
String word = RandomWord.newWord();
int length = word.length();
for(int i = 0; i < length; i++)
{
wordString += "-";
}
System.out.println("The word is: " +wordString);
System.out.println("The secret word is: " +word);
System.out.println("Enter the number of spaces allowed");
int spacesAllowed = keyboard.nextInt();
keyboard.nextLine();
if(spacesAllowed > length)
{
System.out.println("Invalid input. Try again.");
System.out.println("Enter the number of spaces allowed");
spacesAllowed = keyboard.nextInt();
}
while(guessRemaining > 0) {
System.out.println("Please enter the letter you want to guess: ");
String letterGuess = keyboard.next();
char letterCharacter = letterGuess.charAt(0);
System.out.println("Please enter the number of spaces you want to check (seperated by spaces): ");
String spacesChecked = keyboard.next();
boolean guessCheck;
// check if the letter is in the string
guessCheck = (word.indexOf(letterCharacter)) != -1;
if(guessCheck == true)
{
for (int i = 0; i < word.length(); i++) {
if (letterCharacter == word.charAt(i)) {
wordString = wordString.substring(0, i) + letterGuess + wordString.substring(i);
System.out.println("Your guess is in the word!");
System.out.println("The updated word is: " +wordString);
} //end of if statement
} //end of for loop
}
else
{
System.out.println("Your letter was not found in the spaces you provided");
guessRemaining--;
System.out.println("You have " +guessRemaining+ " guesses remaining.");
}
}
if(guessRemaining != 0)
{
System.out.println("You win!");
System.out.println("You have guessed the word! Congratulations");
roundScore = (guessRemaining * 10) / spacesAllowed;
} //end of if
else{
System.out.println("Guesses Remaining: 0");
System.out.println("You have failed to guess the word... :(");
}
System.out.println("Would you like to play again? Yes (y) or No (n)");
String playAgain = keyboard.next();
if(!playAgain.equals("y") && !playAgain.equals("n"))
{
System.out.println("Invalid response, please try again... ");
}
if(playAgain.equals("y"))
{
rounds++;
}
else
{
System.exit(0);
}
}
}
wordString = wordString.substring(0, i) + letterGuess + wordString.substring(i);
if ((wordString.substring(0,i) + wordString.substring(i)).equals(wordString))
System.out.println("These are completely identical");
else
System.out.println("You solved it yourself ;)");
hint: it's 58 in your posted code.
Second part: Completely different program structure.
You'll need to track user's two guessed values, one as a character, the other as an int.
You will compare wordString.toCharArray()[indexUserGuessed] to characterUserGuessed and update the result or game state as needed, using similar code from the way you solved the if statement paradox I provided.
Finally, Welcome to Stack Exchange. MOST of us won't do your homework for you.
Oh and I would look up examples of "StringBuilder Java" as you might find it easier to manipulate your String with this class than with String.
I'm quite new to programming so excuse my basic and limited understanding. Essentially, I am creating a Memory Game for a school project. I want to do 2 do-while loops in a for loop that works like this: the user will be prompted to enter a 4 random letters which will be done in the first do-while loop and the second do-while loop will ask the user to re-input the phrase that they had initially entered.
So my first question is, why does only the first do-while execute? I'm assuming that the for loop executes the first-do-while and than repeats based on my parameters therefore the second one will never execute but, I'd appreciate any help understanding why, and reformatting my program accordingly perhaps.
My second question is that, I want to have a sort of score counter that nets the user 10 points for every correctly guessed letter in the correct sequence and deduct 10 for every incorrect character in the wrong sequence. How would I go about doing so, and what may I have to utilize to make this possible?
Lastly, I would appreciate if anyone could point towards a way of concealing the letters that the user inputs.
Any other suggestions to make my program more efficient would be greatly appreciated!
import java.util.Scanner;
import java.lang.String;
public class MemoryGame {
public static void main(String[]args){
Scanner input = new Scanner(System.in);
int choice;
System.out.println("This is a M E M O R Y G A M E");
System.out.println("Press '1' for instructions");
System.out.println("Press '2' to play");
choice = input.nextInt(); //Checks user selection and redirects
if (choice == 1) {
Instructions();
} else {
playGame();
}
input.close();
}
public static void Instructions() { //Instructions method
int choice;
Scanner input = new Scanner(System.in);
System.out.println("This is a memory game. Below are a few instructions on how to play as well as few hints, and tricks");
System.out.println("> Players wlll be given a input box to input a given number of letters or numbers depending on the stage level.");
System.out.println("> To progress and gain points, users must sucessfully recall the set phrase that they have inputted.");
System.out.println("> Based on the number of correct letters, users will gain points and progress through a set of levels that increase in difficulty.");
System.out.println("> Upon making 3 incorrect character selections users will be displayed a 'Game Over' screen from which they may:");
System.out.println("1. Head to the main menu");
System.out.println("2. View the instructions");
System.out.println("3. Play again");
System.out.println("If users successfully oomplete 5 stages with no errors, they will be prompted a challenge level in which more characters will be required");
System.out.println(" ");
System.out.println("1. Press '1' to return to play");
choice = input.nextInt();
if (choice == 1) {
playGame();
}
input.close(); //Closes input.
}
public static void playGame() {
int userNumbers1;
int userNumbers2;
String userLetters1;
String userLetters2;
int scorePlayer;
int livesPlayer = 3;
int stagePlayer = 4;
int stageGeneral = 1;
Scanner input = new Scanner(System.in);
System.out.println("This is the M E M O R Y G A M E");
System.out.println("Stage 1 initializing . . .");
System.out.println("Please enter " + stagePlayer + " letters of your choice.");
for (int i = 0; i <= 10; i++) {
do {
userLetters1 = input.nextLine();
userLetters1 = userLetters1.toLowerCase(); userLetters1.trim();
if (userLetters1.length()==stagePlayer) {
System.out.println (". . .!");
stagePlayer = stagePlayer + 2;
stageGeneral = stageGeneral + 1;
} else {
System.out.println("Please enter " + stagePlayer + " letters");
}
}
while ( userLetters1.length() != stagePlayer);
do {
userLetters2 = input.nextLine();
userLetters2 = userLetters2.toLowerCase(); userLetters2.trim();
if (userLetters2.length()==userLetters1.length() && userLetters2.equals (userLetters1)) {
System.out.println (". . .");
System.out.println ("Great job!");
System.out.println("Stage " + stageGeneral + " initializing . . .");
System.out.println("Please enter " + stagePlayer + " letters of your choice.");
} else {
System.out.println ("Please enter " + userLetters1.length() + "letters that were previously entered.");
}
}
while ( userLetters1.length() != userLetters2.length());
}
}
}
Don't assume. Debug instead. Look what happens line by line and determine the reason the first do lop never exits. It if exited I see no reason why the second one didn't execute.
For second question I'd enciurage to read String class documentation. All you need is to iterate through characters and react accordingly to results of copmarisions.
In console, to hide previous user input you could clear the screen like:
Runtime.getRuntime().exex("cls);
I've written a portion of code to take a user input, match it to a string value and then use a related double value to make calculations:
double [] currency = new double[] {0.05,0.10,0.20,0.50,1.00,2.00,5.00,10.00,20.00,50.00,100.00};
String [] currencytext = {"$0.05","$0.10","$0.20","$0.50","$1.00","$2.00","$5.00","$10.00","$20.00","$50.00","$100.00"};
Scanner keyboard = new Scanner(System.in);
for (int i = 0; i < currencytext.length; i++) {
boolean valid = false;
while(!valid){
System.out.format("$%.2f remains to be paid. Enter coin or note: ",sum);
String payment = keyboard.next();
if(payment.equals(currencytext[i])){
sum = sum - currency[i];
if(sum == 0) {
System.out.print("You gave " + payment);
System.out.print("Perfect! No change given.");
System.out.print("");
System.out.print("Thank you" + name + ".");
System.out.print("See you next time.");
}
}
if(!(payment.equals(currencytext[i]))) {
System.out.print("Invalid coin or note. Try again. \n");
}
if(payment.equals(currencytext[i]) && currency[i] > sum){
System.out.print("You gave " + payment);
System.out.print("Your change:");
}
}
}
The problem is that when it gets to user input, it doesn't match any string values except for $0.05. It seems to me like its not iterating through the array properly but I can't figure out why. Is anyone able to see a problem here?
This is a possible solution for your problem
Scanner keyboard = new Scanner(System.in);
double [] currency = new double[] {0.05,0.10,0.20,0.50,1.00,2.00,5.00,10.00,20.00,50.00,100.00};
String [] currencytext = {"$0.05","$0.10","$0.20","$0.50","$1.00","$2.00","$5.00","$10.00","$20.00","$50.00","$100.00"};
String payment = keyboard.next();
double sum = 100; // <- Working example - Read sum from keyboard entry
while (sum > 0) {
boolean paymentFound = false;
for (int i = 0; i < currencytext.length; i++) {
if (payment.equals(currencytext[i])) {
sum = sum - currency[i];
paymentFound = true;
if (sum == 0) {
System.out.println("You gave " + payment);
System.out.println("Perfect! No change given.");
// System.out.print("Thank you" + name + ".");
System.out.println("See you next time.");
break;
} else if (sum < 0) {
System.out.println("You gave " + payment);
System.out.println("Your change:" + (-1 * sum));
break;
}
}
}
if (!paymentFound) {
System.out.println("Invalid coin or note. Try again. \n");
}
if (sum > 0) {
System.out.format("$%.2f remains to be paid. Enter coin or note: ", sum);
payment = keyboard.next();
}
}
while-loop will continue until the payment is fullfilled.
for-loop traverse the arrays until a suitable payment is found
If suitable payment is found we substract it from sum. We use break to exit the for-loop in both cases. There is no need to keep searching.
If no suitable payment is found [!paymentFound], we keep on asking.
if (!paymentFound) {
System.out.println("Invalid coin or note. Try again. \n");
}
if (sum > 0) {
System.out.format("$%.2f remains to be paid. Enter coin or note: ", sum);
payment = keyboard.next();
}
The program will end when (sum < 0), in which case the while-loop exits.
I have use println instead of print to improve message legibility.
Too many flaws to point out.
However,
When the currencytext[i] does not match payment, it executes this code:
System.out.print("Invalid coin or note. Try again. \n");
System.out.format("$%.2f remains to be paid. Enter coin or note: ",sum);
payment = keyboard.next();
So, it executes this for all the times that your input does not match currencytext[i].
And, in this block, you have
payment = keyboard.next();
So, it asks for new input, in this block itself. Hence, you get the said output for all inputs except $0.05.
As far as $0.05 is concerned, your first if block executes successfully, and prints no output. So, it moves to the next iteration of the while loop, where again, payment remains the same ($0.05), but currencytext[i] becomes $0.10. SO they do not match, and you get the said output.
How to correct this:
With this code, you need to do a lot of corrections.
I suggest you again start from scratch.
If it doesn't fit, it sets valid to true, so the code just has the chance to check against the first item at currencytext[0], which is $0.05. Then !payment.equals(currencytext[i]) is also true, and your code prints the lines there. Your else ifs are also not properly nested.
I don't know how you are reading input. One improvement you can do is write reading input code in for loop.
Scanner scanner = new Scanner(System.in);
for (... ) {
....
String payment = scanner.nextLine();
....
}
I'm trying to get Java to recognize the output of a while loop as a variable and to use that output in further operations.
I wanted to try and upgrade it by letting one player set the word and the other one guess it. The problem came from making the number of dashes equal to the number of letters in the word that the player entered, so I separated the code out, which worked.
But when I put it all back in main, it would not recognize how many dashes are there after the loop finishes; it only recognizes the initial one which is only 1 dash, and so it poses a problem.
EDIT: Thank you so much guys, its my first time on stack overflow, tnx again.
Works like a charm :D
package iB;
import java.util.Scanner;
import java.lang.String;
public class WordGuess {
/**
* #param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String secretWord ;
String guess, dash = "-", upWord;
int numGuesses = 0;
int numWord;
final String SENTINEL = "!";
System.out.println("Player 2, please look away. Player 1, please enter the secter word: \n");
secretWord = input.next().toUpperCase().trim();
numWord = secretWord.length();
//System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
for(int dashNum = 1; dashNum < numWord; dashNum++) {
dash += "-" ;
}
System.out.println("WordGuess game!\n");
do {
System.out.println("Enter a letter (" + SENTINEL + "to guess entire word): ");
guess = input.next().toUpperCase().trim();
numGuesses ++;
if (secretWord.contains(guess) && guess.length() == 1) {
upWord = dash.substring(0, secretWord.indexOf(guess));
upWord += guess;
upWord += dash.substring(secretWord.indexOf(guess) + 1, dash.length());
dash = upWord.toUpperCase();
System.out.println(dash);
if (dash.equals(secretWord)) {
System.out.println("You won!\n" + "The secret word is " + secretWord);
System.out.println("You made " + numGuesses + " guesses."); }
} else if (guess.length() >= 2) {
System.out.println("Please only enter one letter at a time! \n"); }
if (guess.contains(SENTINEL)) {
System.out.println("What is your guess? ");
guess = input.next().toUpperCase().trim();
if (guess.equals(secretWord)) {
System.out.println("You won!\n" + "The secret word is " + secretWord);
System.out.println("You made " + numGuesses + " guesses.");
break;
} else {
System.out.println("You Lose!");
System.out.println("The secret word was " + secretWord);
System.out.println("You made " + numGuesses + " guesses.");
break;
}
}
} while(!guess.contains(SENTINEL));
input.close();
}
}
The problem
The following piece of code appears to be trying to show where in a word the correctly chosen letter can be found
if (SecretWord.indexOf(guess) >= 0) {
UpWord = dash.substring(0, SecretWord.indexOf(guess));
UpWord += guess;
UpWord += dash.substring(SecretWord.indexOf(guess) + 1, dash.length());
System.out.println(UpWord);
} else {
So if the word was this and you guessed i then the output should be
--i-
dash.substring does not repeat dash, it takes a sub part of dash, as dash is 1 letter long, anything other than substring(0,1) will lead to an exception.
Basic solution
I believe you want to repeat dash until you get to the guessed letter, and then after it till the end of the word. Something along the lines of:
if (SecretWord.indexOf(guess) >= 0) {
int guessedIndex=SecretWord.indexOf(guess);
String outString="";
for(int i=0;i<guessedIndex;i++){
outString+=dash; //repeat dash until we get to the correctly guessed letter
}
outString+=guess; //put the letter in
for(int i=guessedIndex;i<SecretWord.length();i++){
outString+=dash; //repeat dash until we get to end of the word
}
System.out.println(outString);
} else {
Better Solution
This however leaves the problem that only the first instance of the letter is shown. This can be solved using annother stack overflow answer in which we see that we can get all the occurances of a character using a function
public static ArrayList<Integer> getAllIndexes(String testChar, String string){
int index=string.indexOf(testChar);
ArrayList<Integer> indexes=new ArrayList<Integer>();
while(index>0){
indexes.add(index);
index=string.indexOf(testChar,index+1);
}
return indexes;
}
Then using that function to find all the indexes at which the letter occurs we can deal with repeated letters
if (SecretWord.indexOf(guess) >= 0) {
int guessedIndex=SecretWord.indexOf(guess);
ArrayList<Integer> indexes=getAllIndexes(guess,SecretWord);
String outString="";
for(int i=0;i<SecretWord.length();i++){
if (indexes.contains(i)){
outString+=guess; //if its one of the guessed letters, put that in
}else{
outString+=dash; //otherwise add a dash
}
}
System.out.println(outString);
} else {
Now a word of hello and a guess of l correctly outputs --LL-
Notes
It is usual to follow the naming convention that variable names are
in lower camel case, meaning they start with a lower case letter, as
such SecretWord should be secretWord. As it is currently written
it looks like SecretWord is a class, which are usually writen in
upper camel case.
It would be nice, if once you've guessed a letter it stops putting a dash in and starts putting the letter in every time after that, this could be achieved by using an array of booleans to check if the letter has been guessed but that is beyond the scope of this question
All of these solutions have appended strings, which can be slow for huge numbers, in your case this is the right thing to do, but is joining lots of strings together in a loop consider using a StringBuilder to remove the overhead of creating loads of intermediate strings
Solution
If the secret word is pony, the String dash should be equal to ----. The problem is that you never actually change dash from being equal to -. Therefore, when you do things like dash.substring(SecretWord.indexOf(guess) + 1, dash.length()), you get errors because dash only contains one character. Here's how I'd make dash the same length as the secret word:
for(int i = 0; i < NumWord; i++) {
dash += "-";
}
With this one change inserted directly before your do-while loop, your program works like a charm. Below are some other things to consider in order to further improve your program.
Improving readability
Java convention dictates that the first word of method and variable names is lowercase. So NumWord should be numWord, SecretWord should be secretWord, etc.
SecretWord.indexOf(guess) >= 0 should be changed to
SecretWord.contains(guess)
Gameplay suggestions
As in hang man, you should probably show all the spots where the guessed letter occurs. For example, if the secret word is happy, a guess of p should produce the output of --PP- instead of --P--.
As a rule, never accept bad input even if it doesn't cause errors. The program shouldn't allow any of the scenarios below:
A user enters a String containing non-alphabetic characters or multiple words as the secret word
When making guesses, non-alphabetic characters are input (excluding !)
When guessing letters, multiple characters are input.
I have made a couple of modifications to your code, it seems to work fine now.
First though, I added an extra method, just to make it a little easier:
public static String printOutWord(String[] UpWord){
String out = "";
for(int i = 0; i < UpWord.length; i++){
out += UpWord[i];
}
return out;
}
Here are the first few changes to you code:
String[] UpWord = new String[NumWord];
for(int i = 0; i < NumWord; i++){
UpWord[i] = "-";
}
printOutWord(UpWord);
System.out.println("\nWordGuess game!");
So, you no longer need the variable dash, and the variable UpWord has been changed to an array of Strings.
And this is the rest of it:
do {
System.out.println("Enter a letter (! to guess entire word): ");
guess = input.next().toUpperCase().trim();
Numguesses++;
if(guess.length() > 1){
System.out.println("Please only enter one letter at a time");
}else if (SecretWord.indexOf(guess) >= 0) {
int index = SecretWord.indexOf(guess);
UpWord[index] = guess;
while(SecretWord.indexOf(guess, index+1) >= index){
index = SecretWord.indexOf(guess, index+1);
System.out.println(index);
UpWord[index] = guess;
}
System.out.println(printOutWord(UpWord));
if(printOutWord(UpWord).equals(SecretWord)){
System.out.println("You won!\n" + "The secret word is " + SecretWord);
return;
}
} else {
if (guess.contains("!")) {
System.out.println("What is your guess? ");
guess = input.next().toUpperCase();
if (guess.equals(SecretWord)) {
System.out.println("You won!\n" + "The secret word is " + SecretWord);
System.out.println("You made " + Numguesses + " guesses");
} else if (!guess.equals(SecretWord)) {
System.out.println("You Lose!");
System.out.println("You made " + Numguesses + " guesses");
}
}
}
} while (!SecretWord.equals(guess));
input.close();
}
Most of the changes are within the first if statement.
I hope this helped, if any clarification is needed about anything, just ask, I'd be happy to help :)