Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Writing a word jumbler program.

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Writing a word jumbler program.

    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++;
    			}
    		}
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,361
    My Mood
    Mellow
    Thanks
    143
    Thanked 598 Times in 585 Posts

    Default Re: Writing a word jumbler program.


  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: Writing a word jumbler program.

    I am not using separate methods for a reason
    As the poster on the other site said: time to grow up and move on. Programmers do not write code like this. No one should help you with this style of programming.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Reading and Writing Word Documents
    By Esanders in forum Java Theory & Questions
    Replies: 1
    Last Post: April 26th, 2013, 08:48 PM
  2. Word Scramble Program Help
    By beginneratjava in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 18th, 2012, 12:24 PM
  3. How to write code for writing fields into word documents using POI
    By kalpanaathota in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 17th, 2012, 07:19 AM
  4. pls help me with word occurances program
    By kashif in forum Collections and Generics
    Replies: 6
    Last Post: August 16th, 2011, 03:16 PM
  5. pls help me with word occurances program
    By kashif in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2011, 09:39 AM