stdIn.nextLine(); or stdIn.next(); are pulling the incorrect variables
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.
Quote:
Originally Posted by Netbeans Console
How many games should be played in a best of X series? 7
You will be playing 7 matches first one to win 4 games wins.
Player 1: Enter R for Rock, P for Paper, S for Scissors: s
Player 2: Enter R for Rock, P for Paper, S for Scissors: r
S
S
If you notice it doesn't matter what Player 2 inputs, it seems to just automatically pull whatever player 1 put in place.
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?
Code java:
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);
}
}
Re: stdIn.nextLine(); or stdIn.next(); are pulling the incorrect variables
Quote:
Originally Posted by
kamari0
...it doesn't matter what Player 2 inputs, it seems to just automatically pull whatever player 1 put in place.
Hmmm...
Well, did you actually look (really look) at the place where you calculate the playerTwo value?
Quote:
Originally Posted by kamari0 with comment added by Zaphod_b
Code java:
playerTwo = stdIn.nextLine();
playerTwo = playerOne.toUpperCase(); //<--- From Zaphod_b: What the ???
Cheers!
Z