Results 1 to 9 of 9

Thread: Method problems

  1. #1
    Turismo is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default Method problems

    Hello, I have an assignment to make a game. I'm having problems getting a method to recognize a number from the main method. I'm sure it's a gross oversight on my part, but I was wondering if somebody could give me a little guidance on my code.

    This is a standard Rock, Paper, Scissors game that I can get to run in just the main method, but trying to get it to run using separate methods is giving me issues.

    import java.util.*;

    public class RockPaperScissors
    {

    public static void main(String [] args)
    {

    String userInput; //Holds the string for the user input

    System.out.println ("Please Enter R for Rock, P for Paper, S for Scissors: ");
    Scanner keyboard = new Scanner(System.in);
    userInput = keyboard.next();
    userInput = userInput.toUpperCase();
    //helps to ease the process by converting everything to uppercase.


    System.out.println ("Computer throws out a " + computerSwitch());

    }


    public static String computerSwitch()
    {

    Random randomNum = new Random(); //uses the random class create a random object
    int computerNum;
    computerNum = randomNum.nextInt(3); //Randomly generates the number for the computer.

    String computer;

    switch (computerNum) //Assigns the random numbers to an appropriate letter.
    {

    case 0:
    {
    computer = "R"; //Rock
    break;
    }

    case 1:
    {
    computer = "P"; //Paper
    break;
    }

    case 2:
    {
    computer = "S"; //scissors
    break;
    }

    default:
    {
    computer = "ERROR!!!"; //default error.
    }
    }

    return computer;
    }


    public static void computerIf(String userStr, String compStr)
    {
    userStr = userInput; //I'm getting the "cannot find symbol" message here.
    compStr = computerSwitch();


    if (userStr.equals(compStr))
    {
    System.out.println("TIE!!!");
    }
    else if (userStr.equals("R"))
    {
    if (compStr.equals("S"))
    System.out.println("Rock crushes Scissors. WINNER!!!");
    }
    else if (compStr.equals("P"))
    {
    System.out.println ("Paper covers Rock. LOSER!!!");
    }
    else if (userStr.equals("P"))
    {
    if (compStr.equals("S"))
    System.out.println ("Scissor cuts Paper. LOSER!!!");
    }
    else if (compStr.equals("R"))
    {
    System.out.println ("Paper covers Rock. WINNER!!!");
    }
    else if (userStr.equals("S"))
    {
    if (compStr.equals("P"))
    System.out.println ("Scissor cuts Paper. WINNER!!");
    }
    else if (compStr.equals("R"))
    {
    System.out.println ("Rock crushes Scissors. LOSER!!!");
    }
    else
    {
    System.out.println ("Invalid user input.");
    }

    }

    }
    I appreciate all the help anyone can give.

    Thanks.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    You defined a variable 'userInput' in your main( ... ) method; that means the variable is local to that method and it doesn't exist outside of that method. If you want to make the variable 'known' to other methods in your class you either have to pass that value as a parameter to the other method (and, of course, that other method should accept such a parameter) or you define that variable outside of any method, making it an 'instance variable'.

    kind regards,

    Jos

  3. #3
    Turismo is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    Ok, so on your advice, I made a different method to collect the user input for the game.

    The error I get now states that "RockPaperScissors2.java:117: variable result might not have been initialized" It looks to me that in the nested if else statements that I have initialized it.

    import java.util.*;

    public class RockPaperScissors2
    {

    public static void main(String [] args)
    {


    // System.out.print ( computerIf());
    }


    public static String userInputting()
    {
    String userInput; //Holds the string for the user input

    System.out.println ("Please Enter R for Rock, P for Paper, S for Scissors: ");
    Scanner keyboard = new Scanner(System.in);
    userInput = keyboard.next();
    userInput = userInput.toUpperCase(); //helps to ease the process by converting everything to upperacse.

    return userInput;

    }


    public static String computerSwitch()
    {

    Random randomNum = new Random(); //uses the random class create a random object
    int computerNum;
    computerNum = randomNum.nextInt(3); //Randomly generates the number for the computer.

    String computer;

    switch (computerNum) //Assigns the random numbers to an appropriate letter.
    {

    case 0:
    {
    computer = "R"; //Rock
    break;
    }

    case 1:
    {
    computer = "P"; //Paper
    break;
    }

    case 2:
    {
    computer = "S"; //scissors
    break;
    }

    default:
    {
    computer = "ERROR!!!"; //default error.
    }
    }

    return computer;
    }


    public static String computerIf(String userStr, String compStr)
    {
    userStr = userInputting();
    compStr = computerSwitch();
    String result;

    if (userStr.equals(compStr))
    {
    result = ("TIE!!!");
    }
    else if (userStr.equals("R"))
    {
    if (compStr.equals("S"))
    result = ("Rock crushes Scissors. WINNER!!!");
    }
    else if (compStr.equals("P"))
    {
    result = ("Paper covers Rock. LOSER!!!");
    }
    else if (userStr.equals("P"))
    {
    if (compStr.equals("S"))
    result = ("Scissor cuts Paper. LOSER!!!");
    }
    else if (compStr.equals("R"))
    {
    result = ("Paper covers Rock. WINNER!!!");
    }
    else if (userStr.equals("S"))
    {
    if (compStr.equals("P"))
    result = ("Scissor cuts Paper. WINNER!!");
    }
    else if (compStr.equals("R"))
    {
    result = ("Rock crushes Scissors. LOSER!!!");
    }
    else
    {
    result = ("Invalid user input.");
    }
    return result;
    }

    }

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,559
    Rep Power
    25

    Default

    Are you arguing with the compiler? My bet would be on the compiler.
    Why not make it happy by: String result = null;

  5. #5
    Turismo is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    That helped that specific Error, Norm.

    As you can tell, I am still stumbling my way through this. ;)

    So when I go to make a call on the computerIf method, I receive this error.

    RockPaperScissors2.java:17: computerIf(java.lang.String,java.lang.String) in RockPaperScissors2 cannot be applied to ()

    System.out.println (computerIf());
    Here is my main method to try to tie it all together. Is it a syntax error? Programming makes my brain bleed, I swear.

    public static void main(String [] args)
    {
    userInputting();
    System.out.println ("Computer throws out a " + computerSwitch());
    System.out.println (computerIf());
    }

    It will ask me for the input, and will tell me what the computer throws, but it won't give me the win/lose/tie message from the computerIf method.

  6. #6
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    The computerIf() method accepts two String parameters--you are passing no parameters to it. Try passing the results of userInputting and computerSwitch to it.

  7. #7
    Turismo is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    Such as this?

    userStr = userInputting(userInput);
    compStr = computerSwitch(computer);
    I am completely confused on how to input to and call methods apparently. My book feels vague and my professor doesn't really go into detail. He just says it's in the book.

  8. #8
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    No--userInputting and computerSwitch are passed no parameters.

    Imagine a function:
    Java Code:
    public boolean A(String x, String y) {
        // Do something with x and y.
        return true;
    }
    Such a method requires two String parameters to be passed to it, just as your function does. In order to pass the methods, we simply call the function in this manner:
    Java Code:
    String something = "Some string";
    String another = "Another string";
    boolean result = A(something,another); // This is where something and another are "passed" to A.
    Before you read below, I'd suggest you now try it on your own. If you're stumped, continue on.






    In your case, you will have to pass the result of two functions, as seen below:
    Java Code:
    String user = userInputting(); // Notice how this function gets no parameters passed to it.
    String computer = computerSwitch();
    String result = computerIf(user,computer);
    System.out.println(result);
    Hate to kind of do it for you, but unfortunately if you didn't grasp the A(x,y) example there's little more I can do in the manner of teaching without using your example as a direct reference.

  9. #9
    Turismo is offline Member
    Join Date
    Jun 2010
    Posts
    7
    Rep Power
    0

    Default

    Thank you Zack, you explanation is much more clear than what I had been taught previously.

    I worked it out before looking at your example (I'm here to learn first.).

    I am, however, getting the program to ask for my input twice before giving me the answer. I'm going to have to go through and see why this is.

    Please Enter R for Rock, P for Paper, S for Scissors:
    r
    Computer throws a S
    Please Enter R for Rock, P for Paper, S for Scissors:
    r
    Rock crushes Scissors. WINNER!!!

    Thanks for everyone's help, it has been very eye-opening for me. I need to get some different books to help me get this better, I think.

Similar Threads

  1. Problems with Static toString() method
    By RBNSN in forum New To Java
    Replies: 6
    Last Post: 07-09-2010, 10:15 AM
  2. problems with replace method
    By cecily in forum New To Java
    Replies: 1
    Last Post: 08-02-2007, 09:11 PM
  3. BufferedReader: readLine method problems
    By bbq in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2007, 02:27 AM
  4. Problems with method
    By ai_2007 in forum Advanced Java
    Replies: 1
    Last Post: 06-28-2007, 06:49 PM
  5. Problems with Find method in EJB
    By Nick15 in forum Enterprise JavaBeans (EJB)
    Replies: 0
    Last Post: 05-14-2007, 01:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •