Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8799337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:17:52+00:00 2026-06-14T00:17:52+00:00

Here’s the code I have for the core program itself: import java.util.Scanner; import static

  • 0

Here’s the code I have for the core program itself:

import java.util.Scanner;
import static java.lang.System.*;

public class RockPaperScissors
{
private String playChoice;
private String compChoice;

public RockPaperScissors()
{



}

public RockPaperScissors(String player)
{
    playChoice = player;
}

public void setPlayers(String player)
{
    playChoice = player;
    compChoice = "";
    int num;
    num = (int) (Math.random()*3);
    switch(num)
    {
    case 0 : compChoice = "R";break;
    case 1 : compChoice = "P";break;
    case 2 : compChoice = "S";break;
    }
    
    System.out.println(num + " " + compChoice);
    out.print(compChoice);
}

public String determineWinner()
{
    String winner="";
    
    if(playChoice == "R")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Draw Game!";break;
        case "P" : winner = "!Computer wins <<Paper Covers Rock>>!";break;
        case "S" : winner = "!Player wins <<Rock Breaks Scissors>>!";break;
        }
    }
    else if(playChoice == "P")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Player wins <<Paper Covers Rock>>!";break;
        case "P" : winner = "!Draw Game!";break;
        case "S" : winner = "!Computer wins <<Scissors Cuts Paper>>!";break;
        }
    }
    else if(playChoice == "S")
    {
        switch(compChoice)
        {
        case "R" : winner = "!Computer wins <<Rock Breaks Scissors>>!";break;
        case "P" : winner = "!Player wins <<Scissors Cuts Paper>>!";break;
        case "S" : winner = "!Draw Game!";break;
        }
    }
    return winner;
}

public String toString()
{
    String output="";
    
    output = "player had " + playChoice + "\n computer had " + compChoice + "\n " + determineWinner();
    
    return output;
}
}

and here’s my runner class:

import java.util.Scanner;
import static java.lang.System.*;

public class Lab10d
{
public static void main(String args[])
{
    Scanner keyboard = new Scanner(System.in);
    char response  ;
    
    //add in a do while loop after you get the basics up and running
    
        String player = "";
    
        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
        
        //read in the player value
        player = keyboard.next();
    
        RockPaperScissors game = new RockPaperScissors(player);
        game.determineWinner();
        out.println(game);
    while(response.equals('y'))
    {
        out.print("Rock-Paper-Scissors - pick your weapon [R,P,S] :: ");
        player = keyboard.next();
        game.setPlayers(player);
        game.determineWinner();
        out.println(game + "\n");
        out.println("would you like to play again? (y/n):: ");
        response=  keyboard.next();
        
        
        
        
    }
    
}
}

here’s an example of what should be resulting:

Rock-Paper-Scissors – pick your weapon[R,P,S]:: R

player had R

computer had P

!Computer wins <>!

Do you want to play again? y

and if you put n for playing again, it stops.

main problem is getting compChoice to not say null

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:17:53+00:00Added an answer on June 14, 2026 at 12:17 am

    Your code in main method:

    RockPaperScissors game = new RockPaperScissors(player);
    game.determineWinner();
    

    You initialize the compChoice String in the setPlayers() method, but that method is never called. Then you switch on compChoice, which is null, thus you get the NullPointerException.

    This should fix your problem:

    RockPaperScissors game = new RockPaperScissors(player);
    game.setPlayers(player); // <-- insert this line.
    game.determineWinner();
    

    But be aware that you are not saving the String returned by determineWinner(), and then call out.println(game); which will call the determineWinner() method again…

    Also this piece of your code

    while(response == response)

    will always be an endless loop, and i can’t find a break statement in your loop.

    Also NEVER compare Strings with ==, use equals, google the explanation, there even are hundreds of SO explanations for that. So this

    if (playChoice == "R") // <-- WRONG

    should become this:

    if (playChoice.equals("R")) // <-- right

    or

    if ("R".equals(playChoice)) // <-- right

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the code I have. It works. The only problem is that the first
Here's the view: @if (stream.StreamSourceId == 1) { <img class=source src=@Url.Content(~/Public/assets/images/own3dlogo.png) alt= /> }
Here's my code in the <head></head> : <link rel=stylesheet href=http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css /> <script type=text/javascript src=http://code.jquery.com/jquery-1.7.1.min.js></script>
Here is the code in a function I'm trying to revise. This example works
Here is the code: create table `team`.`User`( `UserID` bigint NOT NULL AUTO_INCREMENT , `Username`
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
Here is the two scripts I have Script 1: <? include('config.php'); $json = $_POST['payload'];
Here is an example: I write html code inside of textarea, then I swap
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is an example: I have the generic type called Account. I wish to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.