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 4 of 4

Thread: Skipping user input lines, messing up object generation, and repetition of system.out.println.... I'm just stumped

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Angry Skipping user input lines, messing up object generation, and repetition of system.out.println.... I'm just stumped

    I'm attempting to code an aid for when my little brother and I play a game, and I'm getting a weird, non-fatal, error that you'll see at the end. My main client code attempts to generate two objects, PlayerOne and PlayerTwo. Generating PlayerOne goes just fine, but when I generate PlayerTwo, it just skips lines of code, crucial to the object. My main code is as follows:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package classes;
    import java.util.*;
    /**
     *
     * @author DaedricSheep
     */
    public class Duel {
        public static void main(String[] args) {
            String nameOne, nameTwo;
            int LP1, LP2;
     
            Scanner input = new Scanner(System.in);
     
            System.out.println("Name of Player 1: ");
            nameOne = input.nextLine();
     
            System.out.println("How many Life Points to start?");
            LP1 = input.nextInt();
            Player PlayerOne = new Player(nameOne, LP1);
     
            System.out.println(PlayerOne.toString());
            System.out.println(" ");
     
            System.out.println("Name of Player 2: ");
            nameTwo = input.nextLine();
     
            System.out.println("How many Life Points to start?");
            LP2 = input.nextInt();
            Player PlayerTwo = new Player(nameTwo,LP2);
     
            System.out.println(PlayerTwo.toString());
            System.out.println("");
     
            boolean victory = false;
            while (victory == false){
            System.out.println("IT'S TIME TO DUEL! (type 'help' for options)");
                String action = input.nextLine();
                    switch (action){
                        case "help":
                            System.out.println("Actions:");
                            System.out.println("help, damage, restore, flip coin, guess coin, roll die, victory");
                            System.out.println("");
                            break;
                        case "damage":
                            System.out.println("Was damage was done to player 1 or 2? (enter '1' or '2'");
                            int playerDamage = input.nextInt();
                            if (playerDamage==1){
                                System.out.print("Damage dealt: ");
                                int dealtDam = input.nextInt();
                                PlayerOne.damage(dealtDam);
                                PlayerOne.toString();
                                System.out.println("");
                            }else if(playerDamage==2){
                                System.out.print("Damage dealt: ");
                                int dealtDam = input.nextInt();
                                PlayerTwo.damage(dealtDam);
                                PlayerTwo.toString();
                                System.out.println("");
                            }
                            break;
                        case "restore":
                            System.out.println("Is player 1 or player 2 being healed? (enter '1' or '2'");
                            int playerHeal = input.nextInt();
                            if(playerHeal==1){
                                System.out.print("Health restored: ");
                                int healPoints = input.nextInt();
                                PlayerOne.restore(healPoints);
                                PlayerOne.toString();
                                System.out.println("");
                            }else if(playerHeal==2){
                                System.out.print("Health restored: ");
                                int healPoints = input.nextInt();
                                PlayerTwo.restore(healPoints);
                                PlayerTwo.toString();
                                System.out.println("");
                            }
                            break;
                        case "flip coin":
                            break;
                        case "guess coin":
                            break;
                        case "roll die":
                            InGameActions.dieRoll();
                            break;
                    }
            }
     
        }
    }

    and it refers to the generation of what follows here:

    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
     
    package classes;
     
    /**
     *
     * @author DaedricSheep
     */
    public class Player {
        String playername;
        int LP;
     
        public Player(String name, int Life){
            playername = name;
            LP = Life;
        }
        public int damage(int damage){
            LP = LP - damage;
            return(LP);
        }
        public int restore(int restore){
            LP = LP + restore;
            return(LP);
        }
        public String returnName(){
            return(playername);
        }
        public int returnPoints(){
            return(LP);
        }
        @Override
        public String toString(){
            String that = ("Name: "+returnName()+"   Life Points: "+returnPoints());
            return(that);
        }
    }

    When I run the program, this happens:

    Last edited by DaedricSHeep; April 19th, 2014 at 03:22 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Skipping user input lines, messing up object generation, and repetition of system.out.println.... I'm just stumped

    Clever post, but the video is too small to really see what's going on, however the accompanying description was very good, and it's a typical problem. Notice that this snippet demonstrates the problem adequately, and is all you needed to post:
    import java.util.*;
    /**
     *
     * @author DaedricSheep (snippet of code written by)
     */
    public class TestClass
    {
        public static void main(String[] args) {
            String nameOne, nameTwo;
            int LP1, LP2;
     
            Scanner input = new Scanner(System.in);
     
            System.out.println("Name of Player 1: ");
            nameOne = input.nextLine();
     
            System.out.println("How many Life Points to start?");
            LP1 = input.nextInt();
     
            System.out.println(" ");
     
            System.out.println("Name of Player 2: ");
            nameTwo = input.nextLine();
     
            System.out.println("How many Life Points to start?");
            LP2 = input.nextInt();
     
            System.out.println("");
        }
    }
    The reason the bad behavior is happening is because the nextInt() method leaves a carriage return in the input buffer. Then, when the nextLine() method is called to get the name of Player 2, it takes the carriage return in the input buffer as the input.

    The solution is to "flush the input buffer" and throw it away after the nextInt() method like this:
    import java.util.*;
    /**
     *
     * @author DaedricSheep (snippet of code written by)
     */
    public class TestClass
    {
        public static void main(String[] args) {
            String nameOne, nameTwo;
            int LP1, LP2;
     
            Scanner input = new Scanner(System.in);
     
            System.out.println("Name of Player 1: ");
            nameOne = input.nextLine();
     
            System.out.println("How many Life Points to start?");
            LP1 = input.nextInt();
     
            System.out.println(" ");
     
            // flush the input buffer and throw it away
            input.nextLine();
     
            System.out.println("Name of Player 2: ");
            nameTwo = input.nextLine();
     
            System.out.println("How many Life Points to start?");
            LP2 = input.nextInt();
     
            System.out.println("");
        }
    }

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    DaedricSHeep (April 19th, 2014)

  4. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Skipping user input lines, messing up object generation, and repetition of system.out.println.... I'm just stumped

    Thank you so much, it works like a charm! :! I'll read up on the terminology of what you said, but I got the gist!

    And sorry about the crappy video quality, i'm about 9000% done with OBS =~=

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Skipping user input lines, messing up object generation, and repetition of system.out.println.... I'm just stumped

    Glad to help. I'm sure your video quality would be fine most places, but the tools in this forum don't display graphics and videos well. Not your fault.

Similar Threads

  1. Replies: 1
    Last Post: December 15th, 2013, 12:48 AM
  2. Multiple System.out.println lines to Dialog box.
    By iCurtisIT in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: November 3rd, 2013, 02:55 PM
  3. skipping counting of comments (lines)
    By syedejaz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 16th, 2012, 10:48 AM
  4. How can I have the user input the name of an object?
    By kkid in forum Object Oriented Programming
    Replies: 2
    Last Post: November 12th, 2012, 06:03 PM
  5. Java error while using BufferedReader class to read a .txt document
    By Jchang504 in forum File I/O & Other I/O Streams
    Replies: 6
    Last Post: February 4th, 2009, 07:55 PM