java help very ergant

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • loganandvicky
    New Member
    • Apr 2006
    • 1

    #1

    java help very ergant

    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....

    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;
    	}
    please help me if u can
    Last edited by Niheel; Apr 2 '06, 02:28 AM.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    I can't actually see where the probelm lies. You have not given much detail in you problem description. Where are the variables assigned (did you really mean assigned or did you mean delared), which bit of the code is the bit that goes wrong?

    Comment

    • jagannathan
      New Member
      • Jul 2006
      • 11

      #3
      hi there

      if u have problem calling testInput1 method as it calls variables randomword and input strings.

      Then u might delcare them just after u open ur class and outside any function and it becomes accessible to all the functions

      Hope this solves ur problem

      Comment

      Working...