5 Replies - 336 Views - Last Post: 03 October 2013 - 07:09 PM Rate Topic: -----

#1 johhny1255  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-June 13

rebuilding aprogram that I did months ago. Switch statement problems

Posted 03 October 2013 - 01:03 PM

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.

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)]); //grab random word from file
					
					
					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]); //jumbled characters in word
	}
		

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




Is This A Good Question/Topic? 0
  • +

Replies To: rebuilding aprogram that I did months ago. Switch statement problems

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2203
  • View blog
  • Posts: 5,235
  • Joined: 10-September 10

Re: rebuilding aprogram that I did months ago. Switch statement problems

Posted 03 October 2013 - 02:50 PM

This program is way too complicated to be written entirely inside a single main() method. It's time to grow up a little as a Java programmer and break out some of this code into separate methods. Get the main() method down to a couple lines.

There are more problems than you've mentioned, and discovering and fixing those will be a lot easier when you've broken the code into separate methods with simple, specific tasks that can be easily tested.
Was This Post Helpful? 1
  • +
  • -

#3 johhny1255  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 27-June 13

Re: rebuilding aprogram that I did months ago. Switch statement problems

Posted 03 October 2013 - 04:01 PM

View PostGregBrannon, on 03 October 2013 - 02:50 PM, said:

This program is way too complicated to be written entirely inside a single main() method. It's time to grow up a little as a Java programmer and break out some of this code into separate methods. Get the main() method down to a couple lines.

There are more problems than you've mentioned, and discovering and fixing those will be a lot easier when you've broken the code into separate methods with simple, specific tasks that can be easily tested.

Thanks for your reply. When I made this code I had not learned about methods yet. So now that I am writing it again I still wanted to do it without the use of methods, objects, or classes.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,357
  • Joined: 19-March 11

Re: rebuilding aprogram that I did months ago. Switch statement problems

Posted 03 October 2013 - 04:48 PM

Greg's right, this is way too much to do in main(). main()'s purpose in life is to initialize some needed objects and start the ball rolling. If you do anything significant in main at all, you're just making your life more difficult.

You also really need to get your formatting right - especially if you're going to have a monster main() structure. If your indentation respects the logical flow, it'll be a lot easier to see where your logical flow has gone off the rails.

Quote

Thanks for your reply. When I made this code I had not learned about methods yet. So now that I am writing it again I still wanted to do it without the use of methods, objects, or classes.


The point of learning is that you don't have to do things the old way any more. Apply what you've learned, refactor this into a modular program, and you'll learn even more and be an even better programmer.
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: rebuilding aprogram that I did months ago. Switch statement problems

Posted 03 October 2013 - 07:07 PM

View Postjohhny1255, on 03 October 2013 - 07:01 PM, said:

Thanks for your reply. When I made this code I had not learned about methods yet. So now that I am writing it again I still wanted to do it without the use of methods, objects, or classes.

Wrong approach
Don't try to write more complicated program than 3 or 5 lines of code if you don;t want to create object and use methods
Was This Post Helpful? 1
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7875
  • View blog
  • Posts: 13,357
  • Joined: 19-March 11

Re: rebuilding aprogram that I did months ago. Switch statement problems

Posted 03 October 2013 - 07:09 PM

View Postpbl, on 03 October 2013 - 09:07 PM, said:

View Postjohhny1255, on 03 October 2013 - 07:01 PM, said:

Thanks for your reply. When I made this code I had not learned about methods yet. So now that I am writing it again I still wanted to do it without the use of methods, objects, or classes.

Wrong approach
Don't try to write more complicated program than 3 or 5 lines of code if you don;t want to create object and use methods


Oh, you can go at least 8 lines in main. 12 is a little dicey, though. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1