i have assigned several variables in one method and i want to use them in another method. that works ok until i call the second method. i get java.lang.strin g error
heres my work....
please help me if u can
heres my work....
Code:
/**
Add comment here.
*/
public class WordChaining {
//--------------------------------------------------------------------------
//------------------ ORGANISE GAME UNTIL PLAYER WISHES TO QUIT -------------
//--------------------------------------------------------------------------
public void start() {
boolean notFinished = true;
while ( notFinished ) {
playOneRound();
if ( userWantsToStop() ) {
notFinished = false;
}
}
System.out.println( "BYE" );
}
//--------------------------------------------------------------------------
//------------------ PLAY ONE ROUND ----------------------------------------
//--------------------------------------------------------------------------
private void playOneRound(){
String randomWord;
String input;
String chain;
introductoryMessage();
randomWord = (displayRandomWord());
System.out.println(randomWord);
input = (promptUser());
input = input.toUpperCase();
testInput1();
/** if (randomWord.length() == input.length() && (!randomWord.equals(input))){
System.out.println(true);
}else{
System.out.println(false);
}
int index = 0;
int length = input.length();
while (index < length){
char letter = randomWord.charAt(index);
char letter2 = input.charAt(index);
if (letter == letter2){
System.out.println("YES");
}else{
System.out.println("NO");
}
index++;
}
**/
displayChain();
chain = (randomWord + " " + input);
chain = chain.toUpperCase();
System.out.println(chain);
randomWord = input;
}
//--------------------------------------------------------------------------
//------------------ OBTAIN A RANDOM WORD ----------------------------------
//--------------------------------------------------------------------------
private String getRandomWord( ) {
String theWord = Words.getRandomWord();
return theWord.toUpperCase();
}
//--------------------------------------------------------------------------
//------------------ INTRODUCTORY MESSAGE ----------------------------------
//--------------------------------------------------------------------------
private void introductoryMessage() {
System.out.println("Each new word in the chain can have only one letter different from the previous word in the chain.");
System.out.println("");
System.out.println("");
}
//--------------------------------------------------------------------------
//------------------ DISPLAY RANDOM WORD -----------------------------------
//--------------------------------------------------------------------------
private String displayRandomWord() {
System.out.print("The current wordchain: ");
String randomWord = new String (getRandomWord());
return randomWord;
}
//--------------------------------------------------------------------------
//------------------ PROMPT USER -------------------------------------------
//--------------------------------------------------------------------------
private String promptUser() {
System.out.println("Enter next word");
System.out.print("(press Enter to stop current chain): ");
String input = new String (Keyboard.readInput());
return input;
}
//--------------------------------------------------------------------------
//------------------ DISPLAY CHAIN -----------------------------------------
//--------------------------------------------------------------------------
private void displayChain() {
System.out.println("");
System.out.print("The current word chain: ");
}
//--------------------------------------------------------------------------
//------------------ TEST INPUT --------------------------------------------
//--------------------------------------------------------------------------
private boolean testInput1(){
String randomWord;
String input;
int position = 0;
int length = input.length();
int counter = 0;
/** if (randomWord.length() == input.length() && (!randomWord.equals(input))){
System.out.println("True");
return true;
}else{
System.out.println("False");
return false;
**/
while (position < length){
char letter = randomWord.charAt(position);
char letter2 = input.charAt(position);
if (letter == letter2){
System.out.println("True");
counter++;
}else{
return false;
}
position++;
}
System.out.println(counter);
return true;
}
//--------------------------------------------------------------------------
//------------------ userWantsToStop ---------------------------------------
//--------------------------------------------------------------------------
private boolean userWantsToStop() {
System.out.print( "Press Enter to continue, any other key to finish: " );
String userString = Keyboard.readInput();
if ( userString.equals( "q" ) ) {
return false;
}
return true;
}
Comment