I have two .java files one called GameWord and other NinetenGame the nineteen game should call the classes from the GameWord that i create so it simplifies the code and i can't get it to work i keep getting the nullPointer exception here is the code i have so far. Please help i really need it. Thank You.
Code:package assignment07;
import java.util.Scanner;
/**
* The GameWord objects represent words in the NineteenGuessesGame application.
* The words are stored in the separate objects. The GameWord objects will be used to
* represent the secret word and the users guess.
* <p>
*
* Class contains methods that are validating GameWords.
*
* @author
* @version
* @ta
*
*/
public class GameWord
{
/**
* In this variable the word from the NineteenGuesses Game will be stored.
* <p>
* Abstracting - word: a String containing the word.
*
*/
private String word;
/**
* It builds a GameWord object using a test word.
* <p>
*
* This constructor will not be used in the NineteenGuessesGame
* it will test the GameWord class with specific words.
* <p>
*
* In case the word is empty or null it is replaced with one of strings:
* "Invalid word: empty"
* "Invalid word: null"
* <p>
*
* This is helpful in a way that it will guarantee that the GameWord
* objects meet the representation.
* @param test A word used for testing the GameWord class.
*/
public GameWord(String test)
{
this.word = test;
if(word == null)
word = "Invalid word: null";
if(word.length()== 0)
word = "Invalid word: empty";
}
/**
* This builds a GameWord object from the Scanner object. This allows
* objects to be built from user input or from a file.
* <p>
*
* This is where the Scanner is created that will gather words from either
* keyboard or file input and will be used throughout the NineteenGuessesGame application.
* It will also pass it to the constructor to read words and use them to build objects.
*
* <p>
* In case the word is empty or null it is replaced with one of strings:
* "Invalid word: empty"
* "Invalid word: null"
*
* @param in Input source for the objects word
*/
public GameWord (Scanner in)
{
//Code added later
}
/**
* It compares tow GameWord objects to see if they are the same word. They will
* be equal only if all of the characters match exactly.
* <p>
*
* @param otherWord The object to compare to 'this'
* @return true if 'this' and 'other' are the same word
*/
public boolean equals (GameWord otherWord)
{
if(this.word.equals(otherWord.toString()))
return true;
else
return false;
}
/**
* Returns the String representation of the GameWord.
*
* @return the word, as a String
*/
public String toString()
{
return word;
}
/**
* This will return true if this object is a five-letter lower case word.
* <p>
*
* Otherwise false will be returned if:
* Any character is not a lower case letter.
* And the word is not five characters long.
*
* @return true if it word is valid and false otherwise
*/
public boolean isValid()
{
boolean wordIsValid = true;
for(int i = 0; i < word.toString().length(); i ++)
{
if(!Character.isLowerCase(word.charAt(i)))
wordIsValid = false;
if(word.toString().length() != 5)
wordIsValid = false;
}
return wordIsValid;
}
/**
* This method is used to count and return the number of the correct characters
* that match exactly between 'this' GameWord and some 'other' GameWord. In order for it
* to match exactly characters must be in the same position.
* <p>
*
* In case the 'this' or 'other' are not valid GameWord, the comparison is done and the
* invalid result is returned if valid an integer between 0...5 will be returned.
*
* <p>
*
* @param other the word to compare to 'this'
* @return number of exactly matching characters.
*/
public int countCorrect(GameWord other)
{
String a = word;
String b = other.word;
int correctLetters = 0;
int misplacedLetters = 0;
int index = 0;
while (index < a.length())
{
if (a.charAt(index) == b.charAt(index))
{
correctLetters++;
a = a.substring(0, index) + a.substring(index+1);
b = b.substring(0, index) + b.substring(index+1);
}
else index++;
}
return correctLetters;
}
/**
* In this method the number of characters that are identical but in different
* position between 'this' GameWord and some 'other' GameWord. If the character is repeated
* more than one time it is counted only once. Characters that match and are in same position
* are not counted.
*
* <p>
*
* In case the 'this' or 'other' are not valid GameWord, the comparison is done and the
* invalid result is returned if valid an integer between 0...5 will be returned.
* <p>
*
* @param other the word to compare to 'this'
* @return the number of matching characters that are misplaced.
*/
public int countMissplaced(GameWord other)
{
String a = word;
String b = other.word;
int correctLetters = 0;
int misplacedLetters = 0;
int index = 0;
while (index < a.length())
{
if (a.charAt(index) == b.charAt(index))
{
correctLetters++;
a = a.substring(0, index) + a.substring(index+1);
b = b.substring(0, index) + b.substring(index+1);
}
else index++;
}
int aIndex = 0;
while (aIndex < a.length())
{
int bIndex = 0;
while (aIndex < a.length() && bIndex < b.length())
{
if (a.charAt(aIndex) == b.charAt(bIndex))
{
misplacedLetters++;
a = a.substring (0, aIndex) + a.substring(aIndex+1);
b = b.substring (0, bIndex) + b.substring(bIndex+1);
bIndex = 0;
}
else
bIndex++;
}
aIndex++;
}
return misplacedLetters;
}
}
Moderator Edit: Code tags added to help post's readabilityCode:package assignment07;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/**
* This applications plays a Mastermind-like game
* using five-letter words. The user has nineteen
* guesses to find a secret word. After each guess,
* the game reports how many letters were exactly
* right (and in the correct position), and how many
* letters were right but in the wrong position.
* <p>
*
* Possible words are read from a text file filled
* only with five-letter words.
*
* @author
* @version
*/
public class NineteenGuessesGame1
{
/**
* The Nineteen game application entry point.
* <p>
*
* Note: Command line arguments are ignored.
*
* @param args command line arguments
*/
public static void main (String[] args)
{
// This code is intentionally uncommented.
// It is part of the assignment to figure
// out what this code is doing.
GameWord correctWord = new GameWord("");
try
{
int wordCount = 0;
Scanner s = new Scanner (new File ("five.txt"));
while (s.hasNext())
{
wordCount++;
GameWord fileWord = new GameWord(s);
if(!fileWord.isValid())
{
System.out.println ("The file five.txt contains invalid words. Example: " + fileWord);
s.close();
return;
}
}
s.close();
int wordIndex = (int) (Math.random() * wordCount);
s = new Scanner (new File ("five.txt"));
for (int i = 0; i < wordIndex; i++)
s.next();
correctWord = new GameWord(s);
}
catch (IOException e)
{
System.out.println ("Unable to read the file five.txt - program terminated.");
return;
}
System.out.println ("Welcome to the Nineteen Guesses Game.");
System.out.println ("-------------------------------------");
System.out.println();
System.out.println ("You have nineteen guesses to guess which five-letter word I have selected.");
System.out.println ("After each guess I will tell you how similar your word is to my word.");
System.out.println();
Scanner input = new Scanner(System.in);
int guessNumber = 0;
boolean playerGotIt = false;
while (guessNumber < 19 && !playerGotIt)
{
guessNumber ++;
GameWord userGuess = new GameWord(input);
boolean okGuess = false;
while (!okGuess)
{
System.out.print ("Enter guess # " + guessNumber + ": ");
okGuess = userGuess.isValid();
for (int i = 0; i < userGuess.toString().length(); i++)
if (!Character.isLowerCase(userGuess.toString().charAt(i)))
okGuess = false;
if (userGuess.toString().length() != 5)
okGuess = false;
if (!okGuess)
{
System.out.println ("Invalid entry - try again.");
System.out.println();
}
}
if (userGuess.equals(correctWord))
playerGotIt = true;
else if (guessNumber != 19)
{
int correctLetters = userGuess.countCorrect(correctWord);
int misplacedLetters = userGuess.countMissplaced(correctWord);
System.out.println ("Your guess is incorrect: " + userGuess);
System.out.println (correctLetters + " of your letter(s) are correct and in the correct position.");
System.out.println (misplacedLetters + " of your letter(s) are correct but misplaced.");
System.out.println ("");
}
}
if (playerGotIt)
System.out.println ("Congratulations! You guessed the word in " + guessNumber + " guesses.");
else
System.out.println ("You have used up your nineteen guesses. The word was " + correctWord + ".");
}
}

