#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    I don't know whats wrong.


    So I'm new to java and learning new stuff here and there. I decided to test out my abilities (I just started yesterday) and create an extremely simple RPG character creation text game. I wrote all the code and everything and when I run it it works fine for the first 3 questions. Then when I get to the fourth question. It asks 2 questions at the same time and only lets me answer one. After that all the rest of the questions are screwed up and not answerable.
    Here is the code:

    import java.util.Scanner;

    public class TextGame {
    public static void main(String args[]){
    Scanner player = new Scanner(System.in);
    String name, gender, race, build, path;
    double height;
    int age;

    System.out.println("What is your name?");
    name = player.nextLine();

    System.out.println("What is your gender?");
    gender = player.nextLine();

    System.out.println("How old are you?");
    age = player.nextInt();

    System.out.println("What race are you?");
    race = player.nextLine();

    System.out.println("What type of body build do you have?(Fat, skinny, muscular, etc...)");
    build = player.nextLine();

    System.out.println("How tall are you?(Feet)");
    height = player.nextDouble();

    System.out.println("What class are you?(Warrior, mage, archer, etc...)");
    path = player.nextLine();

    System.out.println("So you are a " + age + "year-old" + build + gender + race + path + "that is " + height + "ft tall, named " + name + "?" );
    player.close();
    }


    }
    //End of RPG test



    Now when I run it this what I get in console, I put x and 1 for place holders:

    What is your name?
    x
    What is your gender?
    x
    How old are you?
    1
    What race are you?
    What type of body build do you have?(Fat, skinny, muscular, etc...)
    x
    How tall are you?(Feet)
    1
    What class are you?(Warrior, mage, archer, etc...)
    x
    So you are a 1 year-old x x x that is 1.0 ft tall, named x.


    What race are you?
    What type of body build do you have?(Fat, skinny, muscular, etc...)
    x
    Problem


    Can someone explain to me why its doing this and tell me how to fix this? Please! Thanks for reading!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    4
    Rep Power
    0
    How's it going xxgetowned!

    The problem with your code is how you're scanning, specifically with player.nextLine();

    Change it to player.next(); and it should be fine. I tested it and it works perfectly.

    Also, I would suggest including necessary spaces for your final output, specifically "+build + gender + race + path+" so it all doesn't get stuck together.

    Cheers!
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,713
    Rep Power
    348
    Also take a look at this page about using the Scanner class:
    http://rommelrico.com/java-scanner-n...-and-nextline/

IMN logo majestic logo threadwatch logo seochat tools logo