This program grabs a random word out of a .txt file and jumble the word and then gives the user 4 options: guess, hint, new word, or quit.
my program compiles and works, however I am having a problem with the switch statement when I try to get a new word
the program grabs a new word and prints it to the screen but keeps the old jumbled word. I want it to jumble the new random word. I used the same code to jumble the word the first time as I do in the switch statement.
I have tried to set the string jumble to null but when I do this the program does not print out anything except the new word.
Any input would be great!
Thanks in advance.
One more note I am not using separate methods for a reason. When I originally wrote this program it was for a school project that I had before we were introduced to methods. I am redoing this project to see if I can improve it.
import java.util.*; import java.util.Random; import java.io.*; public class oldProgram{ public static void main (String [] args)throws IOException{ Scanner s = new Scanner(System.in); Random r = new Random(); Scanner inFile = new Scanner(new File("words.txt")); String jumble2 = ""; String jumble = ""; int count = 0; int points=0; int count1 = 0; int count3 = 0; char letter; char letter2; int partOfWord; int partOfWord2; boolean play = true; int score=0; boolean selection = true; int wordPoints = 10; boolean playGame= true; ArrayList<String> words = new ArrayList<String>(); while(inFile.hasNext()) { words.add(inFile.next()); //read in the words from words.txt file } inFile.close(); while(play == true) { //jumble = "\0"; String[] array = words.toArray(new String [0]); String test1 = (array[new Random().nextInt(array.length)]); char charArray [] = new char [test1.length()]; for(int i=0; i<test1.length(); i++) { letter = test1.charAt(i); partOfWord=r.nextInt(test1.length()); while(count<test1.length()) { if(charArray[partOfWord] == 0) { charArray[partOfWord] = letter; count++; break; } else { partOfWord = r.nextInt(test1.length()); } } } for(int i=0; i<charArray.length; i++) { jumble = jumble + String.valueOf(charArray[i]); } { points=10; System.out.println("current puzzle " + jumble + " " + test1); System.out.println("Current points for word: " + wordPoints); System.out.print("(g)uess, (n)ew word, (h)int, or (q)uit: "); char input = s.next().charAt(0); if(input != 'g' && input!= 'q' && input!= 'h'&&input!= 'n') { System.out.println("Invalid option"); } switch(input) { case 'g': case 'G': System.out.println(); System.out.print("Enter your guess: "); s.nextLine(); String guess = s.nextLine(); if(guess.equals(test1)){ score+=points; guess=test1; System.out.println("You guessed it!"); System.out.println("Score for word: " + wordPoints); } while(!(guess.equals(test1))) { wordPoints--; points--; System.out.println("Nope, Sorry."); System.out.println("current puzzle " + jumble + " " + test1); System.out.println("Current points for word: " + wordPoints); System.out.print("Enter guess"); String guess2 = s.nextLine(); if(guess2.equals(test1) || guess.equals(test1)){ System.out.println("You guessed it"); score+=wordPoints; guess=test1; } } wordPoints=10; break; case 'n': case 'N': jumble2 = ""; String[] array2 = words.toArray(new String [0]); test1 = (array2[new Random().nextInt(array2.length)]); char charArray2 [] = new char [test1.length()]; for(int i=0; i<test1.length(); i++) { letter = test1.charAt(i); partOfWord2=r.nextInt(test1.length()); while(count<test1.length()) { if(charArray2[partOfWord2] == 0) { charArray2[partOfWord2] = letter; count++; break; } else { partOfWord2 = r.nextInt(test1.length()); } } } for(int j=0; j<charArray2.length; j++) { jumble2 = jumble2 + String.valueOf(charArray2[j]); } break; case 'h': case 'H': int hint = r.nextInt(test1.length()); char help = test1.charAt(hint); System.out.println("The letter at spot " + hint + " is " + help); System.out.println("Enter guess"); s.nextLine(); String guess3 = s.nextLine(); if(guess3.equals(test1)){ System.out.println("You guessed it"); score+=wordPoints; guess=test1; } while(!(guess3.equals(test1))) { wordPoints--; points--; System.out.println("Nope, Sorry."); System.out.println("current puzzle " + jumble + " " + test1); System.out.println("Current points for word: " + wordPoints); System.out.print("Enter guess: "); String guess4 = s.nextLine(); if(guess4.equals(test1)){ System.out.println("You guessed it"); score+=wordPoints; guess=test1; wordPoints=10; break; } } break; case 'q': case 'Q': System.out.println("Goodbye!"); System.out.println("Final score: " + score); System.exit(0); break; } count3++; } } } }