I have been stuck on this program for MANY DAYS and I really need help. I am a total NOOB!
I don't know what more to do. I am far too confused.
THIS IS WHAT MY PROGRAM SHOULD OUTPUT: Play the game of Precaution.
Try and create as many unique words as you can using only the letters
in the word 'precaution' where each letter is used at most once.
Words must be at least 3 characters long. Type in the word 'exit'
to exit the program.
Guess a word using the letters in "precaution": caution
That is word # 1
Guess a word using the letters in "precaution": turn
That is word # 2
Guess a word using the letters in "precaution": in
Sorry, words must be longer than 3 characters.
Guess a word using the letters in "precaution": near
That is word # 3
Guess a word using the letters in "precaution": peer
Sorry, you used an invalid letter.
Guess a word using the letters in "precaution": real
Sorry, you used an invalid letter.
Guess a word using the letters in "precaution": exit
Thanks for playing. You found a total of 3 words.
THIS IS THE CODE I HAVE SO FAR
// Import libraries needed by the program
import java.util.Scanner; // used for getting input from the keyboard
public class Precaution {
// Fields that can be accessed anywhere in the class go here
Scanner keyboard = new Scanner(System.in); // used to read user input
public static void main(String[] args) {
// create an instance of this class
Precaution thePrecautionInstance = new Precaution();
// call a non-static method to do everything else
thePrecautionInstance.mainLoop();
}
void mainLoop() {
String userInput = ""; // used to store user input
System.out.println();
// Display game instructions
System.out.println("Play the game of Precaution. \n"
+ "Try and create as many unique words as you can using only the letters \n"
+ "in the word 'precaution' where each letter is used at most once. \n"
+ "Words must be at least 3 characters long. Type in the word 'exit' \n"
+ "to exit the program. \n");
// Using a loop, show how to branch back up to the top of the loop
// Main loop
boolean notDone = true;
while (notDone) {
// display prompt and get user input
System.out.println();
System.out.print("Type in any word, or \"exit\" to exit this loop: ");
userInput = keyboard.next().toLowerCase(); // read in user input &
// convert to lower case
// handle user input of "exit"
if (userInput.equals("exit")) {
notDone = false;
continue; // branch back up to top of loop. Since notDone is now
// false, the loop will be exited.
}
// handle user input of "out"
if (userInput.equals("out")) {
break; // break out of the enclosing loop and go to line
// following the loop
}
System.out.println("Still in the loop...");
}// end while( notDone)...
// Get the length of the user input
int theLength = userInput.length();
if (theLength <= 4) {
System.out.println("You typed in: " + userInput + ". That word is 4 letters long or less.");
}
// Use a loop to individually get and then display each letter of the
// input word
int characterIndex = 0;
while (characterIndex < userInput.length()) {
char currentLetter = userInput.charAt(characterIndex);
System.out.print(currentLetter);
characterIndex++; // advance to the next character index
}
System.out.println(); // leave a blank line in the output
// In a String replace a particular character with a blank character
String originalWord = "precaution";
System.out.println("Original word is: " + originalWord);
// OK, now eliminate the letter 'c' and test to see if it worked
char theLetter = 'c';
String newWord = "";
newWord = originalWord.replace(theLetter, ' '); // do the replacement
// See if it was found and replaced by comparing the two Strings
if (newWord.equals(originalWord)) {
// Strings are equal, so letter was NOT fond
System.out.println("Character " + theLetter + " was NOT found.");
} else {
// Strings are different, so letter WAS found and eliminated
System.out.println("Character " + theLetter + " was found and eliminated.");
}
System.out.println("Resulting string is: " + newWord);
// Repeat the above code, this time with a character that isn't there
theLetter = 'q';
newWord = originalWord.replace(theLetter, ' '); // do the replacement
// See if it was found and replaced by comparing the two Strings
if (newWord.equals(originalWord)) {
// Strings are equal, so letter was NOT fond
System.out.println("Character " + theLetter + " was NOT found.");
} else {
// Strings are different, so letter WAS found and eliminated
System.out.println("Character " + theLetter + " was found and eliminated.");
}
System.out.println("Resulting string is: " + newWord);
// Show how we can see if a long list of words contains a particular
// word
String allWords = "";
allWords = allWords + " " + "first";
allWords = allWords + " " + "second";
allWords = allWords + " " + "third";
allWords = allWords + " " + "fourth";
System.out.println("List of words is: " + allWords);
System.out.print("Enter word to search for: ");
userInput = keyboard.next(); // read in a user input word to search for
if (allWords.contains(userInput)) {
// word was found
System.out.println(userInput + " WAS found in word list.");
} else {
// word was not found
System.out.println(userInput + " not found in word list.");
}
System.out.println("Done with program.");
}// end mainLoop()
}// end class Precaution