I have an assignment that I have been working on that is to modify an existing rock paper scissors program so that I ahve a method that sets the player name and a method that gets the player's name. I have created the methods however it does not allow me to input the player name it skips to where it asks for the amount of rounds.

The output dialogue looks like this;
"What is your name? How many rounds? 3
Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): 1
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]throw ROCK.
Computer throws SCISSORS.
You win!
Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): 2
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]throw PAPER.
Computer throws ROCK.
You win!
Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): 3
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\,][decimal separator=.][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]throw SCISSORS.
Computer throws SCISSORS.
It's a draw!
You win!"

This is the code that I am using;

         /*
         * RPS2.java from Chapter 8
         * An object-oriented version of Rock Paper Scissors
         * Lawrenceville Press
         * June 10, 2005
         */

 import java.util.Scanner;

 /**
 * Computer plays Rock Paper Scissors against one player.
 */
            rounds = input.nextInt();
            for (int i = 0; i < rounds; i++) {
                    System.out.print("Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): ");
                    playerThrow = input.nextInt();
                    rpsOpponent.makeThrow(playerThrow);

                    rps.makeCompThrow();
                    rps.announceWinner(rpsOpponent.getThrow());
            }
            rps.bigWinner();
    }
    static String name;
    public static String assignName() {
        System.out.print("What is your name? ");
        Scanner nameinput = new Scanner(System.in);
        name = nameinput.toString();
        return RPS2.name;
    }
    public String getName(){
        return RPS2.name;
    }
 }


    /**
     * models the player in a game of RPS
     */
    public class RPSPlayer {
            private int playerThrow;                                                        //ROCK = 1, PAPER = 2, SCISSORS = 3

        /**
         * constructor
         * pre: none
         * post: RPSPlayer object created. The player is given a default throw.
         */
        public RPSPlayer() {
                playerThrow = 1;        //default throw
        }


    /**
     * Sets the player's throw.
     * pre: newThrow is the integer 1, 2, or 3.
     * post: Player's throw has been made.
     */
     public void makeThrow(int newThrow){
            playerThrow = newThrow;
     }


    /**
     * Returns the player's throw.
     * pre: none
     * post: Player's throw has been returned.
     */
     public int getThrow() {
            return(playerThrow);
     }

}



/**
 * Models a game of RPS
 */

import java.util.Random;

public class RPSGame {
        public static final int ROCK = 1, PAPER = 2, SCISSORS = 3;
        private int compThrow;
        private int playerWins = 0, computerWins = 0;

        /**
         * constructor
         * pre: none
         * post: RPSGame object created. Computer throw generated.
         */
        public RPSGame() {
            Random rand = new Random();

            compThrow = rand.nextInt(3) + 1;                //random integer between 1 and 3
            playerWins = 0;
            computerWins = 0;
    }


    /**
     * Computer's throw is generated (1 for ROCK, 2 for PAPER, 3 for SCISSORS).
     * pre: none
     * post: Computer's throw generated.
     */
     public void makeCompThrow(){
            Random rand = new Random();

            compThrow = rand.nextInt(3) + 1;                //random integer between 1 and 3
     }


    /** 
     * Returns the computer's throw.
     * pre: none
     * post: Computer's throw has been returned.
     */
    public int getCompThrow() {
            return(compThrow);
    }


    /**
     * Determines the winner of the round.
     * pre: playerThrow is the integer 1, 2, or 3.
     * post: Displays a message indicating throws. Compares player's throw 
     * to computer's throw and displays a message indicating the winner.
     */
     public void announceWinner(int playerThrow) {

            RPS2 rps2= new RPS2();
            System.out.print(rps2.getName() + " throws ");
            switch (playerThrow) {
                    case ROCK: System.out.println("ROCK."); break;
                    case PAPER: System.out.println("PAPER."); break;
                    case SCISSORS: System.out.println("SCISSORS."); break;
            }
            System.out.print("Computer throws ");
            switch (compThrow) {
                    case ROCK: System.out.println("ROCK."); break;
                    case PAPER: System.out.println("PAPER."); break;
                    case SCISSORS: System.out.println("SCISSORS."); break;
            }

            /* Determine and annouce winner */
            if (playerThrow == ROCK && compThrow == ROCK) {
                    System.out.println("It's a draw!");
            } else if (playerThrow == ROCK && compThrow == PAPER) {
                    System.out.println("Computer wins!");
                    computerWins += 1;
            } else if (playerThrow == ROCK && compThrow == SCISSORS) {
                    System.out.println("You win!");
                    playerWins += 1;
            }

            if (playerThrow == PAPER && compThrow == ROCK) {
                    System.out.println("You win!");
                    playerWins += 1;
            } else if (playerThrow == PAPER && compThrow == PAPER) {
                    System.out.println("It's a draw!");
            } else if (playerThrow == PAPER && compThrow == SCISSORS) {
                    System.out.println("Computer wins!");
                    computerWins +=1;
            }

            if (playerThrow == SCISSORS && compThrow == ROCK) {
                    System.out.println("Computer wins!");
                    computerWins += 1;
            } else if (playerThrow == SCISSORS && compThrow == PAPER) {
                    System.out.println("You win!");
                    playerWins += 1;
            } else if (playerThrow == SCISSORS && compThrow == SCISSORS) {
                    System.out.println("It's a draw!");
            }
     }




    /** 
     * Displays the overall winner.
     * pre: none
     * post: Computer and player wins compared and 
     * an overall winner announced.
     */
    public void bigWinner() {
            if (computerWins > playerWins){
                    System.out.println("Computer wins!");
            } else if (playerWins > computerWins){
                    System.out.println("You win!");
            } else {
                    System.out.println("It's a draw!");
            }
    }

}

Recommended Answers

All 18 Replies

Scanner nameinput = new Scanner(System.in);
name = nameinput.toString

You are trying to use the Scanner itself as a name! You're supposed to read the name using the Scanner.

There is missing code before line 13. I'd expect to see a class definition and a method there.

I have editted the program in an effort to make it work, here is the modified RPS2 class;

/*
 * RPS2.java from Chapter 8
 * An object-oriented version of Rock Paper Scissors
 * Lawrenceville Press
 * June 10, 2005
 */

 import java.util.Scanner;

 /**
 * Computer plays Rock Paper Scissors against one player.
 */
 public class RPS2 {

        public static void main(String[] args) {
                String name;
                name=assignName();
                RPSGame rps = new RPSGame();
                RPSPlayer rpsOpponent = new RPSPlayer();

                int playerThrow;
                Scanner input = new Scanner(System.in);

                /* play RPS */
                System.out.println(name);
                for (int i = 0; i < numRounds(); i++) {
                        System.out.print("Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): ");
                        playerThrow = input.nextInt();
                        rpsOpponent.makeThrow(playerThrow);
                        rps.makeCompThrow();
                        rps.announceWinner(rpsOpponent.getThrow());
                }
                rps.bigWinner();
        }
        public static int numRounds(){
            int rounds;
            Scanner input = new Scanner(System.in);
            System.out.print("How many rounds? ");
            rounds = input.nextInt();
            return rounds;
        }

        public static String assignName() {
            System.out.print("What is your name? ");
            Scanner input = new Scanner(System.in);;
            String name = input.toString();
            return name;
        }
        public static Scanner getName(){
            System.out.println(name);
        }
 }

The error I am getting is that name cannot be resolved, I am not sure how to declare the name variabl to be called by other classes and methods any help is great.

Member Avatar for joankim

Again, you are triyng to assign the scanner itself to name. this is like doing

new scanner(System.in).toString

Not a great idea, right? :)

So try this instead:

 Scanner input = new Scanner(System.in);;
         String name = input.next();
         return name.toString();

return name.toString();

The toString() call is redundant. name is a String.

Thanks joankim, I have editted the code as shown below however I am still completely lost as to how I can print the name variable at will by using the getName method. Any help in pointing me to a java doc explaining it or your own explanation would be helpful.

EDIT: I have two ideas now, I could change the main into a non static method or I could make these both static getters and setters, both of which I am completely lost in solving them.

/*
 * RPS2.java from Chapter 8
 * An object-oriented version of Rock Paper Scissors
 * Lawrenceville Press
 * June 10, 2005
 */

 import java.util.Scanner;

 /**
 * Computer plays Rock Paper Scissors against one player.
 */
 public class RPS2 {


        public static void main(String[] args) {
                RPSGame rps = new RPSGame();
                RPSPlayer rpsOpponent = new RPSPlayer();

                int playerThrow;
                Scanner input = new Scanner(System.in);
                assignName();
                /* play RPS */
                getName(); //this line is to test if it works
                for (int i = 0; i < numRounds(); i++) {
                        System.out.print("Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): ");
                        playerThrow = input.nextInt();
                        rpsOpponent.makeThrow(playerThrow);
                        rps.makeCompThrow();
                        rps.announceWinner(rpsOpponent.getThrow());
                }
                rps.bigWinner();
        }
        public static int numRounds(){
            int rounds;
            Scanner input = new Scanner(System.in);
            System.out.print("How many rounds? ");
            rounds = input.nextInt();
            return rounds;
        }

        public static String assignName() {
            System.out.print("What is your name? ");
            Scanner input = new Scanner(System.in);
            String name = input.next();
            return name.toString();
        }
        public static void getName(){
            return name; //unclear how to get the name
            System.out.println(name);
        }
 }

Where is the name saved? In what variable in what class?

If its the name variable defined in the assignName() method, you need to move the definiton of name to the class level out of the method.

I fixed your code this works, but you will need to edit your other classes to call upon the getname() method.

When you are calling getname() use System.out.println(getname()); since it is returning a string.

`

import java.util.Scanner;

public class RPS2 {

 static String name;

    public static void main(String[] args) {
            RPSGame rps = new RPSGame();
            RPSPlayer rpsOpponent = new RPSPlayer();
            int playerThrow;
            Scanner input = new Scanner(System.in);
            assignName();
            /* play RPS */
            System.out.println(getName()); //this line is to test if it works
            for (int i = 0; i < numRounds(); i++) {
                    System.out.print("Enter your throw (ROCK=1, PAPER=2, SCISSORS=3): ");
                    playerThrow = input.nextInt();
                    rpsOpponent.makeThrow(playerThrow);
                    rps.makeCompThrow();
                    rps.announceWinner(rpsOpponent.getThrow());
            }
            rps.bigWinner();
    }
    public static int numRounds(){
        int rounds;
        Scanner input = new Scanner(System.in);
        System.out.print("How many rounds? ");
        rounds = input.nextInt();
        return rounds;
    }
    public static String assignName() {
        System.out.print("What is your name? ");
        Scanner input = new Scanner(System.in);
        name = input.next();
        return name.toString();
    }
    public static String getName(){
        return name; 
    }

}
`

commented: spoonfeeding code -3

Why are you doing the OPs homework? You don't learn how to code by copying and pasting.

L Spoon feeding h r Text

Well we are in the same class doing the same work,
I will explain it to him tomorrow morning.

I have learned a lot from other people's code, not exactly copy and pasting but reading and understanding how everything goes together.
It is not completely useless, unless you are a complete fool.

Thanks for the link, will definitly check it out.

Norm, please stop posting unless you are posting something helpful, otherwise it is just spam

Are you trying to learn programming
or are you just looking for working code?

Member Avatar for diafol

Norm, please stop posting unless you are posting something helpful, otherwise it is just spam

If you look at Norm's posts they have been helpful - or at least he's been trying to help you. You've decided to ignore him until your last post. Posting a complete solution is not always the best way to help an OP.

I have an idea: how about we actually contribute to the topic, instead of whoring posts?

Any help in pointing me to a java doc explaining it or your own explanation would be helpful.

EDIT: I have two ideas now, I could change the main into a non static method or I could make these both static getters and setters, both of which I am completely lost in solving them.>

It really looks like I am trying to get the code spoon fed doesn't it?

Member Avatar for diafol

Whoring posts? V funny +1 from me.
@j
Nobody's accusing you of anything. Can I suggest that we get back to the point of looking for a solution. And I take it that j wants pointers not a complete solution as highlighted in the last post.

Thanks for repeating exactly what I said post whore

The roblem has been solved, thanks to those who actully helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.