Jumble anagram game

Returning NULL when read from file :(

Page 1 of 1

10 Replies - 9864 Views - Last Post: 21 November 2010 - 04:00 PM Rate Topic: -----

#1 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Jumble anagram game

Posted 16 November 2010 - 07:05 PM

Ahoy there mates! I am consulting the o'wise Java board with a debugging problem, I can't seem to make this return anything! the first print statement returns a null and the second isnt reached due to a NullPointerException. wordlist.txt contains a dictionary of words. It is attached if anyone is curious.

What this code is *supposed* to do is pull the document into a string array (dictionary), get a random word of length WORDLENGTH, jumble it, and print it out. Later im going to add a timer and turn it into a "make as many words as you can" game, but thats for later. For now is the null value being returned. I've been stumbling around with this for hours now and drawing a blank. Help a n00b CS major and I'll give you a pie.
,
import java.util.*;
import java.awt.*;
import java.io.*;
public class Words{
	public static final int WORDLENGTH = 6;
	public static final int ARRAYSIZE = 22394;
	public static void main ( String[] args)
	throws FileNotFoundException{
		boolean game = true;
		while(game) {
			String[] dictionary = readFile();
			dictionary = condenseArray(dictionary);
			Random r = new Random();
			int ind = r.nextInt(dictionary.length-1);
			String sWord = dictionary[ind];
			System.out.println(sWord);
			char[] cWord = jumble(sWord);
			System.out.print("The "+ WORDLENGTH+" letters are :");
			for(int i = 0; i < cWord.length; i++){
				System.out.print(cWord[i]);
			}
		}
	}
	//jumbles a string and returns it as a char array.
	public static char[] jumble(String s){
		Random r = new Random();
		char[] str = s.toCharArray();
		int length = str.length;
		int i;
		int other = 0;
		char temp = 0;
		for(i=0;i<length-1;i++){
		  //swap two random characterss
		  temp = str[i];
		  other = r.nextInt(length);
		  str[i] = str[other];
		  str[other] = temp;
		}
		return str;
	}
	//reads the array out of wordlist.txt
	public static String[] readFile()
	throws FileNotFoundException{

		String[] tbr = new String[ARRAYSIZE];
		File file = new File("wordlist.txt");
		Scanner fin = new Scanner(file);
		String temp = null;
		int sum = 0;
		while(fin.hasNext()){
			temp = fin.next();
			if(temp.length()==WORDLENGTH){
				tbr[sum] = temp;
				sum++;
			}
		}
		return tbr;
	}

	//trims all the excess NULL references off of a string array
	public static String[] condenseArray(String[] dictionary){
		boolean endoffill = false;
		int sum = 0;
		while(!endoffill){
			if(dictionary[sum]==null){
				endoffill = true;
			}
		sum++;
		}
		return new String[sum-1];
	}
}

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Jumble anagram game

#2 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Jumble anagram game

Posted 16 November 2010 - 07:21 PM

First, the nextInt() method should not have dictionary.length - 1 because the method returns a value from 0 INCLUSIVE and the parameter EXCLUSIVE.

Your problem is...your condenseArray() method.
- Notice you are returning a new String array...and that array is never filled up again. Thus, the array you recieve from that method is all null values.

You probably want to determine the number of words in the array first before setting the elements equal to the words.
- make another loop and go through with hasNext() to determine the number of lines in your file. That should be the size of the array.
- Then make another loop that actually goes about assigning the words.
Was This Post Helpful? 0
  • +
  • -

#3 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: Jumble anagram game

Posted 16 November 2010 - 07:24 PM

Oh wow, thanks so much! I feel like an idiot now!!! Thank you a ton! Now I can keep working! Here is your pi:
________    ____   _____ ____ .________________________  
\_____  \  /_   | /  |  /_   ||   ____/   __   \_____  \ 
  _(__  <   |   |/   |  ||   ||____  \\____    //  ____/ 
 /       \  |   /    ^   /   |/       \  /    //       \ 
/______  / /\___\____   ||___/______  / /____/ \_______ \
       \/  \/        |__|           \/                 \/


Was This Post Helpful? 0
  • +
  • -

#4 bcranger  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,199
  • Joined: 01-February 10

Re: Jumble anagram game

Posted 16 November 2010 - 07:35 PM

Yummy :^:
Happy coding!
Was This Post Helpful? 0
  • +
  • -

#5 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: Jumble anagram game

Posted 20 November 2010 - 07:53 PM

Eep! Back to the drawing board! got a little further before getting stumped again this time...anyone want to give this one a shot?

import java.util.*;
import java.awt.*;
import java.io.*;
public class Words{
	public static final int WORDLENGTH = 6;
	public static final int ARRAYSIZE = 22394;
	public static Random r = new Random();
	public static Scanner s = new Scanner(System.in);
	public static void main ( String[] args)
	throws FileNotFoundException{
		//keeps a letter count of the word
		String loopinput = "y";
		String[] enteredWords = new String[factorial(WORDLENGTH)];
		while(loopinput.equals("y")||loopinput.equals("Y")) {
			int words = 0;
			int[] letterInstance = new int[26];
			String gameinput = null;
			String[] dictionary = readFile();
			String sWord = pullLetterWord(dictionary);
			char[] cWord = jumble(sWord);
			letterInstance = fillLetter(cWord);
			System.out.print("The "+ WORDLENGTH+"-letters are :");
			for(int i = 0; i < cWord.length; i++){
				System.out.print(cWord[i]);
			}
			double starttime = System.currentTimeMillis();
			System.out.println("\nNow enter your words (Type a zero to quit)");
			boolean playing = true;
			//game starts here\\
			while(playing){
				gameinput = s.next();
				if(System.currentTimeMillis()>starttime+150000 ||
				gameinput.equals("0")){
					playing = false;
					continue;
				}
				if(checkWord(gameinput.toCharArray(), letterInstance,enteredWords,
				dictionary)){
					enteredWords[words] = gameinput;
					words++;
					System.out.println("Good!");
				}
				else{
					System.out.println("Not really!");
				}

			}
			System.out.println("Sorry! Out of time!\n");
			System.out.println("\tUSER");
			System.out.println(words+" words");
			for(int j = 0; j < words+1; j++)
				System.out.println(enteredWords[j]);
			System.out.println("\nPlay again? y/n");
			loopinput = s.next();
		}
	}
	//increments the indice in letters for each
	//instance of that letter(ie if theres 2 instances
	//of "a", letters[0] will be 2.
	public static int[] fillLetter(char[] word){
		int[] letters = new int[26];
		int ind;
		char letter;
		for(letter = 'A'; letter < 'Z'; letter++){
			for(ind = 0; ind < word.length; ind++){
				if(word[ind]==letter)
					letters[letter-65]++;
			}
		}
		return letters;
	}
	//checks if a letter is right
	public static boolean checkWord(char[] word, int[] letters,
	String[] enteredWords,String[] dictionary){
		boolean tbr = false; //tbr, false will be replaced
		for(int i = 0; i < enteredWords.length; i++){
			if(enteredWords[0]!=null){
				if(enteredWords[i].equals(word)){
					tbr = false;
				}
			}
		}
		for(int i = 0; i<word.length; i++){
			letters[(int)word[i]-97]--;
			if(letters[(int)word[i]-97]<0)
				tbr = false;
		}
		for(int i=0; i<ARRAYSIZE; i++){
			if(word.equals(dictionary[i]))
				tbr = true;
				continue;
		}
		return tbr;
	}
	//finds the factorial for the size of the entered words
	//array (maximum number of combinations is the factorial
	//of the length of the word.)
	public static int factorial( int n )
	    {
	        if( n <= 1 )     // base case
	            return 1;
	        else
	            return n * factorial( n - 1 );
    }
	//jumbles a string and returns it as a char array.
	public static char[] jumble(String s){
		Random r = new Random();
		char[] str = s.toCharArray();
		int length = str.length;
		int i;
		int other = 0;
		char temp = 0;
		for(i=0;i<length-1;i++){
		  //swap two random characters
		  temp = str[i];
		  other = r.nextInt(length);
		  str[i] = str[other];
		  str[other] = temp;
		}
		return str;
	}
	//reads the array out of wordlist.txt
	public static String[] readFile()
	throws FileNotFoundException{

		String[] tbr = new String[ARRAYSIZE];
		File file = new File("wordlist.txt");
		Scanner fin = new Scanner(file);
		String temp = null;
		int sum = 0;
		while(fin.hasNext()){
			temp = fin.next();
			tbr[sum] = temp;
				sum++;
		}
		return tbr;
	}
	//pulls a word of WORDLENGTH length from dictionary.
	public static String pullLetterWord(String[] dictionary){
		int ind = 0;
		String sWord;
		do{
			ind = r.nextInt(dictionary.length);
			sWord = dictionary[ind];
		}while (sWord.length()!=WORDLENGTH);
		return sWord;
	}
}



same dictionary file. It outputs "Not Really!" Every time, hinting that theres a problem in the checkWord method. Halp!

Thanks.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Jumble anagram game

Posted 20 November 2010 - 09:44 PM

Love the comments within your code
Was This Post Helpful? 0
  • +
  • -

#7 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: Jumble anagram game

Posted 20 November 2010 - 11:05 PM

I'm assuming that was sarcasm ;_;
Was This Post Helpful? 0
  • +
  • -

#8 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: Jumble anagram game

Posted 21 November 2010 - 01:27 PM

pbl, I'm loving the bit in your signature about the 0.5, wouldn't that make life so much easier? ;)
Was This Post Helpful? 0
  • +
  • -

#9 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: Jumble anagram game

Posted 21 November 2010 - 03:18 PM

I'm still sitting here staring at this. I have no idea what's wrong, I've looked over this method atleast a hundred times and I can't figure it out... :(

It's something in checkWords, I know that. I can't tell what's wrong though.

word is the user input converted to a character array.

letters is an array of instances of letters within word. IE, if the word was "attack", letters[0] should be 2, (A)tt(A)ck. That part is working as far as i know.

enteredWords is all the words that are currently in the array, except that isn't working because even words that SHOULD be working are returning as false within checkWord.

dictionary is a string array read from wordslist.txt
Was This Post Helpful? 0
  • +
  • -

#10 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: Jumble anagram game

Posted 21 November 2010 - 04:00 PM

I broke the method checkWord into three:
	public static boolean isinDictionary( char[] gameinput, String[] dictionary){
		boolean tbr = false;
		for(int i = 0; i < dictionary.length; i++){
			if(dictionary[i].equals(String.valueOf(gameinput))){
				tbr = true;
			}
		}
		return tbr;
	}
	//checks if a letter is in EnteredWords
	public static boolean checkEntered(char[] word, int words,
	String[] enteredWords/*,String[] dictionary*/){
		boolean tbr = true;
		if(enteredWords[0] != null){
			for(int i = 0; i < words; i++){
				if(enteredWords[i].equals(String.valueOf(word))){
					continue;
				}
			}
		}
		else{
			tbr = true;
		}
		return tbr;
	}
	public static boolean isInWord(char[] word, int[] letters){
		boolean tbr = true;
		for(int i = 0; i < 26; i++){
			for(int j = 0; j < word.length; j++){
				if(word[j]==(char)(letters[i+'a'])){
					letters[i]--;
				}
			}
		}
		for(int i = 0; i < 26; i++){
			if(letters[i] < 0){
				tbr=false;
			}
		}
		return tbr;

	}


Still doing the same thing.

heres the current total source code.

////////////////////
//words.java
//Richard Dobson
//words.java is an anagram game where
//the user makes words out of jumbled
//letters in 2 and a half minutes.
//
//CSC110 Java Programming
//Dr Zerangue
//11AM-12PM Block


import java.util.*;
import java.awt.*;
import java.io.*;
public class Words{
	public static final int WORDLENGTH = 6;
	public static final int ARRAYSIZE = 22394;
	public static Random r = new Random();
	public static Scanner s = new Scanner(System.in);
	public static void main ( String[] args)
	throws FileNotFoundException{
		//keeps a letter count of the word
		String loopinput = "y";
		String[] enteredWords = new String[factorial(WORDLENGTH)];
		while(loopinput.equals("y")||loopinput.equals("Y")) {
			int words = 0;
			int[] letterInstance = new int[26];
			String gameinput = null;
			String[] dictionary = readFile();
			String sWord = pullLetterWord(dictionary);
			char[] cWord = jumble(sWord);
			letterInstance = fillLetter(cWord);
			System.out.print("The "+ WORDLENGTH+"-letters are :");
			for(int i = 0; i < cWord.length; i++){
				System.out.print(cWord[i]);
			}
			double starttime = System.currentTimeMillis();
			System.out.println("\nNow enter your words (Type a zero to quit)");
			boolean playing = true;
			//game starts here\\
			while(playing){
				gameinput = s.next();
				if(System.currentTimeMillis()>starttime+150000 ||
				gameinput.equals("0")){
					playing = false;
					continue;
				}
				if(!checkEntered(gameinput.toCharArray(), words,enteredWords)&&
					isInWord(gameinput.toCharArray(),letterInstance)&&
					isinDictionary(gameinput.toCharArray(),dictionary))
				{
					enteredWords[words] = gameinput;
					words++;
					System.out.println("Good!");
				}
				else{
					System.out.println("Not really!");
				}

			}
			System.out.println("Sorry! Out of time!\n");
			System.out.println("\tUSER");
			System.out.println(words+" words");
			for(int j = 0; j < words+1; j++)
				System.out.println(enteredWords[j]);
			System.out.println("\nPlay again? y/n");
			loopinput = s.next();
		}
	}
	//increments the indice in letters for each
	//instance of that letter(ie if theres 2 instances
	//of "a", letters[0] will be 2.
	public static int[] fillLetter(char[] word){
		int[] letters = new int[26];
		int ind;
		char letter;
		for(letter = 'A'; letter < 'Z'; letter++){
			for(ind = 0; ind < word.length; ind++){
				if(word[ind]==letter)
					letters[letter-65]++;
			}
		}
		return letters;
	}
	public static boolean isinDictionary( char[] gameinput, String[] dictionary){
		boolean tbr = false;
		for(int i = 0; i < dictionary.length; i++){
			if(dictionary[i].equals(String.valueOf(gameinput))){
				tbr = true;
			}
		}
		return tbr;
	}
	//checks if a letter is in EnteredWords
	public static boolean checkEntered(char[] word, int words,
	String[] enteredWords/*,String[] dictionary*/){
		boolean tbr = true;
		if(enteredWords[0] != null){
			for(int i = 0; i < words; i++){
				if(enteredWords[i].equals(String.valueOf(word))){
					continue;
				}
			}
		}
		else{
			tbr = true;
		}
		return tbr;
	}
	public static boolean isInWord(char[] word, int[] letters){
		boolean tbr = true;
		for(int i = 0; i < 26; i++){
			for(int j = 0; j < word.length; j++){
				if(word[j]==(char)(letters[i+'a'])){
					letters[i]--;
				}
			}
		}
		for(int i = 0; i < 26; i++){
			if(letters[i] < 0){
				tbr=false;
			}
		}
		return tbr;

	}
	//finds the factorial for the size of the entered words
	//array (maximum number of combinations is the factorial
	//of the length of the word.)
	public static int factorial( int n )
	    {
	        if( n <= 1 )     // base case
	            return 1;
	        else
	            return n * factorial( n - 1 );
    }
	//jumbles a string and returns it as a char array.
	public static char[] jumble(String s){
		Random r = new Random();
		char[] str = s.toCharArray();
		int length = str.length;
		int i;
		int other = 0;
		char temp = 0;
		for(i=0;i<length-1;i++){
		  //swap two random characters
		  temp = str[i];
		  other = r.nextInt(length);
		  str[i] = str[other];
		  str[other] = temp;
		}
		return str;
	}
	//reads the array out of wordlist.txt
	public static String[] readFile()
	throws FileNotFoundException{

		String[] tbr = new String[ARRAYSIZE];
		File file = new File("wordlist.txt");
		Scanner fin = new Scanner(file);
		String temp = null;
		int sum = 0;
		while(fin.hasNext()){
			temp = fin.next();
			tbr[sum] = temp;
				sum++;
		}
		return tbr;
	}
	//pulls a word of WORDLENGTH length from dictionary.
	public static String pullLetterWord(String[] dictionary){
		int ind = 0;
		String sWord;
		do{
			ind = r.nextInt(dictionary.length);
			sWord = dictionary[ind];
		}while (sWord.length()!=WORDLENGTH);
		return sWord;
	}
}

Was This Post Helpful? 0
  • +
  • -

#11 Rikarus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 11-March 09

Re: Jumble anagram game

Posted 21 November 2010 - 04:00 PM

*SNIP* Sorry, it posted twice.

This post has been edited by Rikarus: 21 November 2010 - 04:01 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1