First off I hope I am in the correct section, I am currently writing a Rock, Paper, Scissors program for my class and I ran into an issue that I don't seem to be able to fix.
If you notice it doesn't matter what Player 2 inputs, it seems to just automatically pull whatever player 1 put in place.Originally Posted by Netbeans Console
I have tried adding
stdIn.next();
stdIn.nextInt();
stdIn.nextLine();
To clear the stream, however it doesn't seem to resolve the issue and just forces the user to type on a blank line to proceed. Anyone have any tips?
package assignment4; import java.awt.AWTException; import java.awt.Robot; import java.util.Scanner; import java.util.logging.Level; import java.util.logging.Logger; public class Assignment4 { public static void main(String[] args) { String playerOne; String playerTwo; int userInput; int gameCount; boolean bestOf = false; Scanner stdIn = new Scanner(System.in); //Best of X Series while (!bestOf) { System.out.print("How many games should be played in a best of X series? "); gameCount = stdIn.nextInt(); userInput = gameCount % 2; if (userInput == 1) { System.out.println("You will be playing " + gameCount + " matches first one to win " + ((gameCount / 2) + 1) + " games wins."); bestOf = true; } else { System.out.println("You have entered an invalid number for a best of X series, please enter a new number"); } } //Clean out the stream stdIn.nextLine(); //Turn phase //PlayerOne's Turn System.out.print("Player 1: Enter R for Rock, P for Paper, S for Scissors: "); playerOne = stdIn.nextLine(); playerOne = playerOne.toUpperCase(); //Robot to Clear Screen, Pulled from [url=http://stackoverflow.com/questions/7522022/how-to-delete-stuff-printed-to-console-by-system-out-println]java - How to delete stuff printed to console by System.out.println()? - Stack Overflow[/url] try { Robot clearScreen = new Robot(); clearScreen.keyPress(17); // Holds CTRL key. clearScreen.keyPress(76); // Holds L key. clearScreen.keyRelease(17); // Releases CTRL key. clearScreen.keyRelease(76); // Releases L key. } catch (AWTException ex) { Logger.getLogger(Assignment4.class.getName()).log(Level.SEVERE, null, ex); } //PlayerTwo's Turn System.out.print("Player 2: Enter R for Rock, P for Paper, S for Scissors: "); playerTwo = stdIn.nextLine(); playerTwo = playerOne.toUpperCase(); //Testing Variable System.out.println(playerOne); System.out.println(playerTwo); } }