views:

35

answers:

1

I have a user input their name as a string and then the name is printed out onto the screen. How can i limit what is printed to only 12 characters so that a user cannot type an insanely long name? Here is my code:

    Scanner input = new Scanner(System.in);
    System.out.print("Enter your player name: ");
    String name= input.next();
    System.out.print("\n" + name + " has started the game\n");
+2  A: 

Something like:

String name = input.next();
name = name.length() > 12 ? name.substring(0, 11) : name;

and accept some of your previous answers.

Chris Dennett
thank you all for your help. the substring works great.
David
Cool :) Please click on the tick next to the answer.
Chris Dennett