Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Program with transfer value from one class to another

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Program with transfer value from one class to another

    I am trying to create a Who Wants To Be A Millionaire Game - just like the Tv show. However, I keep getting an error message saying "returnValue cannont be resolved to a variable". I am just wondering how to solve the problem when passing the value back to the game class from computer class.

    I am using eclipse to write the source code.

    Part of the Game Class
    boolean correctAnswer = true;
     
    		while (correctAnswer == true) {
     
    			String pAnswer = player.getUserInput("Enter a number");
     
    			String rAnswer = computer.checkAnswerQ1(returnValue);
     
    			if (rAnswer.equals("Incorrect")) { 
     
    				correctAnswer = false;
     
    				System.out.println("THAT IS THE WRONG Answer. The answer you where looking for was:" + rAnswer);
    			}
    Computer Class
    public class Computer {
     
    	public Integer checkAnswerQ1(String intAnswer) {
     
    		int num = Integer.parseInt(intAnswer);
    		int playerAnswer = num;
     
    		int[] index = new int[4];
     
    		index[0] = 0;
    		index[1] = 1;
    		index[2] = 2;
    		index[3] = 3;
     
    		String[] RealAnswer = new String[4];
     
    		RealAnswer[0] = "Incorrect";
    		RealAnswer[1] = "Incorrect";
    		RealAnswer[2] = "Correct";
    		RealAnswer[3] = "Incorrect";
     
    		String returnValue = "Incorrect";
     
    		index[num] = playerAnswer; // assigning the playerAnswer as number value which is en look 
    		// up by the string array to give either correct or incorrect answer
     
    		int actualAnswer = 3; // correct answer for the question
     
    		System.out.println("Host: Your Answer is:" + RealAnswer[playerAnswer]);
    		System.out.println("Host: Final Answer?");
    		System.out.println("Player: Final Answer");
    		System.out.println("For 500 pounds the answer is:");
     
    		if(playerAnswer == actualAnswer) {
     
    			System.out.println("A London"); 
    			System.out.println("Congraulations you have just won 500 pounds");
     
    			} else if (playerAnswer != actualAnswer) {
     
    					System.out.println("The corred answer is" + actualAnswer);
    					System.out.println("You just lost 500 pounds");
    			} // closes the else if statement
     
    		return returnValue;
    	} // closes the method
    } // closes the class
    Last edited by helloworld922; January 5th, 2011 at 02:24 PM.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Program with transfer value from one class to another

    Pretty sure you want to be using pAnswer instead of returnValue.

    Use code tags.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program with transfer value from one class to another

    Ok I have changed the return in the computer class to playerAnswer which got rid of one of the problem, however, for the

    String rAnswer = computer.checkAnswerQ1(playerAnswer);

    it is still saying: playerAnswer cannot be resolved to a variable

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Program with transfer value from one class to another

    pAnswer != playerAnswer
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Program with transfer value from one class to another

    Hi,

    I have changed the playerAnswer to pAnswer, however, it still displays the same error below is the full code if this is of any help.

    Game Class
    public class Game {
     
    	public static void main(String[] args) {
     
    		Player player = new Player(); 
     
    		Computer computer = new Computer();
     
    		// introduction to the game
    		System.out.println("Hello and welcome to another show of Who Wants To Be a Millionaire");
    		System.out.println("Tonight we have a new contestan, hoping to get their hands on jackpot of ONE MILLION POUNDS");
    		System.out.println("There is just one problem. Between him and the money are sixteen questions");
    		System.out.println("");
    		System.out.println("Ok First i'll explain the prize breakdown");
    		System.out.println("");
    		System.out.println("> The first question is worth: 		£500");
    		System.out.println("> The second question is worth: 	£1,000");
    		System.out.println("> The third question is worth: 		£2,000");
    		System.out.println("> The fouth question is worth: 		£3,000");
    		System.out.println("> The fifth question is worth:		£4,000 with a gurantee of atleast going home with this sum of money");
    		System.out.println("> The sixth question is worth:		£5,000 ");
    		System.out.println("> The seventh question is worth: 	£10,000");
    		System.out.println("> The eight question is worth: 		£15,000");
    		System.out.println("> The nineth question is worth: 	£18,000");
    		System.out.println("> The tenth question is worth: 		£23,000 with a gurantee of atleast going home with this sum of money");
    		System.out.println("> The eleveth question is worth: 	£28,000");
    		System.out.println("> The twevleth question is worth: 	£37,000");
    		System.out.println("> The thirteenth question is worth:	£50,000");
    		System.out.println("> The fourteenth question is worth:	£125,000");
    		System.out.println("> The fiftheenth question is worth:	£250,000");
    		System.out.println("> The sixteenth question is worth: 	£1,000,000");
     
    		// beginning of the game
    		System.out.println("");
    		System.out.println("Ok, lets begin.");
     
    		// first question
     
    		System.out.println("");
    		System.out.println("Question 1");
    		System.out.println("");
    		System.out.println("Ok here the first question for 500 pounds");
    		System.out.println("What is the captial of England?");
    		System.out.println("> Is it A: Paris?");
    		System.out.println("> Is it B: Washington DC?");
    		System.out.println("> Is it C: London?"); // correct answer to question
    		System.out.println("> or is it D: Moscow?");
     
    		// User input
     
    		boolean correctAnswer = true;
     
    		while (correctAnswer == true) {
     
    			String pAnswer = player.getUserInput("Enter a number");
     
    			String rAnswer = computer.checkAnswerQ1(pAnswer);
     
    			if (rAnswer.equals("Incorrect")) { 
     
    				correctAnswer = false;
     
    				System.out.println("THAT IS THE WRONG Answer. The answer you where looking for was:" + rAnswer);
     
    					Stop stop = new Stop();
    						stop.stopProgram();
     
    			}
    		}  	
    	}
    }
    Computer Class

    public class Computer {
     
    	public Integer checkAnswerQ1(String intAnswer) {
     
    		int num = Integer.parseInt(intAnswer);
    		int playerAnswer = num;
     
    		int[] index = new int[4]; // integer index for numbers 
     
    		index[0] = 0;
    		index[1] = 1;
    		index[2] = 2;
    		index[3] = 3;
     
    		String[] RealAnswer = new String[4]; // string array for finding the correct answer
     
    		RealAnswer[0] = "Incorrect";
    		RealAnswer[1] = "Incorrect";
    		RealAnswer[2] = "Correct";
    		RealAnswer[3] = "Incorrect";
     
    		index[num] = playerAnswer; // assigning the playerAnswer as number value which is then look 
    		// up by the string array to give either correct or incorrect answer
     
    		int actualAnswer = 3; // correct answer for the question
     
    		System.out.println("Host: Your Answer is:" + RealAnswer[playerAnswer]); // prints the message plus the user response
    		System.out.println("Host: Final Answer?"); // message
    		System.out.println("Player: Final Answer"); // message
    		System.out.println("Host: Ok, for 500 pounds the answer is: "); // message
     
     
    		if(playerAnswer == actualAnswer) { // tests to see if the player is the same a the actual answer to the question
     
    			System.out.print("A London"); // user message
    			System.out.println("Host: Congraulations you have just won 500 pounds"); // user message
     
    			} else if (playerAnswer != actualAnswer) { // if the player and actual answer aren't the same it throws an error / incorrect answer
     
    					System.out.println("The correct answer is" + RealAnswer[actualAnswer]); // user message
    					System.out.println("You just lost 500 pounds"); // user message
    			} // closes the else if statement
     
    		return pAnswer; // returns the value of playerAnswer
    	} // closes the method
    } // closes the class
    Last edited by helloworld922; January 5th, 2011 at 02:23 PM.

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Program with transfer value from one class to another

    You still have to use code tags.

    Post the full stack trace, and point out any line numbers referenced in the stack trace so we don't have to count manually.

    Also, your code should be in the form of an SSCCE (that's a link). So take out all of the extra stuff, throw everything into one class, and make sure it demonstrates the problem.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Program with transfer value from one class to another

    The program takes a users reponse to the question, which can be 1 to 4. After the number is tested via looking in the computer class to see if the playerAnswer and actucalAnswer match. If they do the program moves onto the next question, if they don't, the program displays a message and closes the program.

    If you like i can attach the src file so you can run the program.

    Where the errors are appearing are shown in red.

    P.s. I am not sure i have used the code tags properly.

    [code]
    boolean correctAnswer = true;

    while (correctAnswer == true) {

    String pAnswer = player.getUserInput("Enter a number");

    String rAnswer = computer.checkAnswerQ1( playerAnswer);

    if (rAnswer.equals("Incorrect")) {

    correctAnswer = false;

    System.out.println("THAT IS THE WRONG Answer. The answer you where looking for was:" + rAnswer);

    Stop stop = new Stop();
    stop.stopProgram();

    [code]
    public class Computer {

    public Integer checkAnswerQ1(String intAnswer) {

    int num = Integer.parseInt(intAnswer);
    int playerAnswer = num;

    int[] index = new int[4]; // integer index for numbers

    index[0] = 0;
    index[1] = 1;
    index[2] = 2;
    index[3] = 3;

    String[] RealAnswer = new String[4]; // string array for finding the correct answer

    RealAnswer[0] = "Incorrect";
    RealAnswer[1] = "Incorrect";
    RealAnswer[2] = "Correct";
    RealAnswer[3] = "Incorrect";

    index[num] = playerAnswer; // assigning the playerAnswer as number value which is then look
    // up by the string array to give either correct or incorrect answer

    int actualAnswer = 3; // correct answer for the question

    System.out.println("Host: Your Answer is:" + RealAnswer[playerAnswer]); // prints the message plus the user response
    System.out.println("Host: Final Answer?"); // message
    System.out.println("Player: Final Answer"); // message
    System.out.println("Host: Ok, for 500 pounds the answer is: "); // message


    if(playerAnswer == actualAnswer) { // tests to see if the player is the same a the actual answer to the question

    System.out.print("A London"); // user message
    System.out.println("Host: Congraulations you have just won 500 pounds"); // user message

    } else if (playerAnswer != actualAnswer) { // if the player and actual answer aren't the same it throws an error / incorrect answer

    System.out.println("The correct answer is" + RealAnswer[actualAnswer]); // user message
    System.out.println("You just lost 500 pounds"); // user message
    } // closes the else if statement

    return playerAnswer; // returns the value of playerAnswer
    } // closes the method
    } // closes the class
    [code]

  8. #8
    Member goldest's Avatar
    Join Date
    Oct 2009
    Location
    Pune, India
    Posts
    63
    Thanks
    1
    Thanked 12 Times in 10 Posts

    Wink Re: Program with transfer value from one class to another

    You really need to know how to use code tags!

    Then, you need to pass the pAnswer to your checkAnswerQ1 method. Because pAnswer is what your user has selected as an answer.


    String rAnswer = computer.checkAnswerQ1(pAnswer);
    Your actual checkAnswerQ1 method is returning an Integer, and you are trying to store it in String, why and how?
    public Integer checkAnswerQ1(String intAnswer)
    You are comparing the outcome of checkAnswerQ1 with String, so it should return a String like this,
    public String checkAnswerQ1(String intAnswer)
    Now in this method, have a String variable defaulted to "Incorrect". When you evaluate your pAnswer, if it matches with the actual answer, make that String "Correct" or else leave it as it is. Finally return that String.

    Hope that helps,

    Goldest
    Java Is A Funny Language... Really!

    Sun: Java Coding Conventions

    Click on THANKS if you like the solution provided.
    Click on Star if you are really impressed with the solution.

Similar Threads

  1. Two class program help
    By BuhRock in forum Object Oriented Programming
    Replies: 4
    Last Post: July 4th, 2012, 05:27 PM
  2. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  3. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  4. new program (lotto.class)
    By james137 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2009, 05:22 PM
  5. Replies: 1
    Last Post: September 10th, 2009, 10:54 AM

Tags for this Thread