-1

Ok, hi every one. This is my first time here on stackoverflow, I've doing a lot of searching here so I figure that this would be a good place to get some answers. I am a beginner programmer currently in school. I am working on my assignment of creating a rock, paper, scissors game. I feel like my code is good so far, I compiled it but it wont run at all. Take a look:

    import java.util.Scanner;
    public class RPSGame {


    public static void gameModeSelect ()
    {
    System.out.println("Welcome to Rock, Paper, Scissors 1.0 !\n Please select your game mode: ");
    System.out.println("1. Player vs. Computer\n 2. Player vs. Player ");
    }

    public static void winLoss ()
    {
    int P1 = 0, P2 = 0;

    if (P1 == 'P' && P2 == 'R'){
        System.out.println("Paper covers rock!\nPlayer one wins!");
    } else if (P1 == 'R' && P2 == 'P'){
        System.out.println("Paper covers rock!\nPlayer two wins!");
    }    
    if (P1 == 'R' && P2 == 'S'){
        System.out.println("Rock breaks scissors!\nPlayer one wins!");
    } else if (P1 == 'S' && P2 == 'R'){
        System.out.println("Rock breaks scissors!\nPlayer two wins!");
    }
    if (P1 == 'S' && P2 == 'P'){
        System.out.println("Scissor cuts paper!\nPlayer one wins!");
    } else if (P1 == 'P' && P2 == 'S'){
        System.out.println("Scissor cuts paper!\nPlayer two wins!");
    }
   }

    public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);  

    int P1;
        P1 = 0;
    int P2;
        P2 = 0;
    int modeSelect;
    modeSelect = keyboard.nextInt();

    gameModeSelect ();
    if (modeSelect == 1){
        System.out.println("Oops, this feature in currently unavailable. Play with a friend for now :-)");
    } else if (modeSelect == 2){

        System.out.println("Rules of the game:  R = Rock, P = Paper, S = Scissors\n Good luck! ");

        System.out.println("Player one: Enter your move");
        P1 = keyboard.nextInt();
        System.out.println("Player two: Enter your move");
        P2 = keyboard.nextInt();

    } else if (modeSelect > 2){
        winLoss();

    }

  }
}

It compiles without errors but nothing runs, however when I enter anything in the running section I get this error:

run:
S
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:864)
    at java.util.Scanner.next(Scanner.java:1485)
    at java.util.Scanner.nextInt(Scanner.java:2117)
    at java.util.Scanner.nextInt(Scanner.java:2076)
    at RPSGame.main(RPSGame.java:41)
C:\Users\AVLG2\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 12 seconds)

I'm stuck here, and my assignment is due tomorrow. Any suggestions? It would be highly appreciated. BTW, glad to be part of the stackoverflow community!!

Lego91
  • 41
  • 4

3 Answers3

2

You don't see anything because in main method you invoke

modeSelect = keyboard.nextInt();

before printing anything. This call stops main thread until user will provide some data to read. If data will be of type int flow of control will move you to next line, if it will not be int exception will be thrown (like in your case since R is not valid int).

To let user know what is going on print message which explains that program is waiting for his input:

System.out.print("Please provide game mode (<mode explanation here>): ");
modeSelect = keyboard.nextInt();

Also when you are asking user for characters you can't use nextInt() because R is not int. Use next() instead.


In case of problems also read:

How do I compare strings in Java?
Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods
What is a stack trace, and how can I use it to debug my application errors?

Community
  • 1
  • 1
Pshemo
  • 113,402
  • 22
  • 170
  • 242
2
public static void gameModeSelect() {
    System.out.println("Welcome to Rock, Paper, Scissors 1.0 !\n Please select your game mode: ");
    System.out.println("1. Player vs. Computer\n 2. Player vs. Player ");
}

public static void winLoss(char P1, char P2) {

    if (P1 == 'P' && P2 == 'R') {
        System.out.println("Paper covers rock!\nPlayer one wins!");
    } else if (P1 == 'R' && P2 == 'P') {
        System.out.println("Paper covers rock!\nPlayer two wins!");
    }
    if (P1 == 'R' && P2 == 'S') {
        System.out.println("Rock breaks scissors!\nPlayer one wins!");
    } else if (P1 == 'S' && P2 == 'R') {
        System.out.println("Rock breaks scissors!\nPlayer two wins!");
    }
    if (P1 == 'S' && P2 == 'P') {
        System.out.println("Scissor cuts paper!\nPlayer one wins!");
    } else if (P1 == 'P' && P2 == 'S') {
        System.out.println("Scissor cuts paper!\nPlayer two wins!");
    }
}

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    char P1;
    char P2;
    int modeSelect;

    gameModeSelect();
    modeSelect = keyboard.nextInt();

    if (modeSelect == 1) {
        System.out.println("Oops, this feature in currently unavailable. Play with a friend for now :-)");
    } else if (modeSelect == 2) {

        System.out.println("Rules of the game:  R = Rock, P = Paper, S = Scissors\n Good luck! ");

        System.out.println("Player one: Enter your move");
        P1 = keyboard.next().charAt(0);
        System.out.println("Player two: Enter your move");
        P2 = keyboard.next().charAt(0);
        winLoss(P1, P2);

    }
}
  1. You have to choose the game mode: modeSelect = keyboard.nextInt();
  2. Read the players inputs: P1 = keyboard.next().charAt(0); (& P2 row)
  3. Evaluate the game: invoke the winLoss function with appropiate values
Ursache
  • 196
  • 8
  • I'm not sure if this reply is permitted since I read "there's only questions and answers." But thank you! I originally attempted to make P1 & P2 char but could not get it to work. Your suggestion of "keyboard.next().charAt(0);" was the key that I couldn't find. Thanks again. I think I got it from here! – Lego91 May 21 '17 at 22:16
1

As already mentioned Scanner#nextInt() can only parse Integers entered by users. For characters it throws an exception.

Here your code with minor changes to make it work:

public class RPSGame {

public static void gameModeSelect() {
    System.out.println("Welcome to Rock, Paper, Scissors 1.0 !\n Please select your game mode: ");
    System.out.println("1. Player vs. Computer\n 2. Player vs. Player ");
}

public static void winLoss() {
    int P1 = 0, P2 = 0;

    if (P1 == 'P' && P2 == 'R') {
        System.out.println("Paper covers rock!\nPlayer one wins!");
    } else if (P1 == 'R' && P2 == 'P') {
        System.out.println("Paper covers rock!\nPlayer two wins!");
    }
    if (P1 == 'R' && P2 == 'S') {
        System.out.println("Rock breaks scissors!\nPlayer one wins!");
    } else if (P1 == 'S' && P2 == 'R') {
        System.out.println("Rock breaks scissors!\nPlayer two wins!");
    }
    if (P1 == 'S' && P2 == 'P') {
        System.out.println("Scissor cuts paper!\nPlayer one wins!");
    } else if (P1 == 'P' && P2 == 'S') {
        System.out.println("Scissor cuts paper!\nPlayer two wins!");
    }
}

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);

    String P1 = null;
    String P2 = null;
    int modeSelect;

    gameModeSelect();
    modeSelect = keyboard.nextInt();
    if (modeSelect == 1) {
        System.out.println("Oops, this feature in currently unavailable. Play with a friend for now :-)");
    } else if (modeSelect == 2) {

        System.out.println("Rules of the game:  R = Rock, P = Paper, S = Scissors\n Good luck! ");

        System.out.println("Player one: Enter your move");
        P1 = keyboard.next();
        System.out.println("Player two: Enter your move");
        P2 = keyboard.next();

    } else if (modeSelect > 2) {
        winLoss();

    }

}
}

The evaluation is missing, but I guess that's up to you.

Marteng
  • 741
  • 6
  • 22