Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: stdIn.nextLine(); or stdIn.next(); are pulling the incorrect variables

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

    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);
     
        }
    }


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: stdIn.nextLine(); or stdIn.next(); are pulling the incorrect variables

    Quote Originally Posted by kamari0 View Post
    ...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
            playerTwo = stdIn.nextLine();
            playerTwo = playerOne.toUpperCase(); //<--- From Zaphod_b: What the ???


    Cheers!

    Z

Similar Threads

  1. Differences between local variables and instance variables
    By rob17 in forum Java Theory & Questions
    Replies: 2
    Last Post: March 6th, 2012, 08:34 PM
  2. [SOLVED] help pulling information from different classes when you have more than one
    By mickey2012 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2012, 10:38 PM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. Pulling Data from Website
    By aussiemcgr in forum Java Theory & Questions
    Replies: 0
    Last Post: August 20th, 2010, 08:42 AM
  5. Pulling in data from access
    By tdc5013 in forum JDBC & Databases
    Replies: 2
    Last Post: March 15th, 2010, 04:40 PM