Jump to content

Issue concerning nextLine() in java

kcchi
                while(temp <= numOfPlayers){

                    System.out.print(userNamePrompt.replace(NUMBER, "#" + temp));

 -->               String name = console.nextLine();

                    players[temp-1].setName(name);

                    temp++;

                }

 

numOfPlayers = 2

NUMBER = "%NUMBER"

userNamePrompt = "Name of racer %NUMBER"

console = Scanner(System.in)

temp = 1

players  = arrays of objects of type Player

 

The output I want is:

 

Name of racer #1: , then waits for my input

Name of racer #2: , then waits for my input again

 

What I get with nextLine() is:

 

Name of racer #1: Name of racer #2: , then waits for my input (only takes 1 input, not 2)

 

Basically it goes through the while loop twice before asking me for an input. I know it works with the next() method but the problem is when the user inputs a name with a space in between, i.e. Sponge Bob, which would put Sponge in name 1 and Bob in name 2. Why doesn't nextLine() work in this case and how do I make it so it takes Sponge Bob as one name? Thanks guys

CPU: Intel i7-4770k MOBO: Asus Maximus VI Formula RAM: G.Skill RipJawsX 2x8GB 1600Mhz GPU: Asus GTX 780 DCuII CASE: Phanteks Enthoo Primo STORAGE: ADATA SX900 256GB / WD Caviar Blue 1TB PSU: Corsair AX760 760W MONITOR: Dell Ultrasharp U2312HM KEYBOARD: Ducky Shine 3 MOUSE: Logitech G602 http://linustechtips.com/main/topic/106614-project-insert-name-enthoo-primo-rigid-acrylic-red/
Link to comment
Share on other sites

Link to post
Share on other sites

  • Your use of a while loop when you want a temporary variable just for the loop, have a condition to break it and something to increment for that condition is pretty bad. Use a for-loop for that since it covers all those bases.
  • The whole having a prompt is nice, but replacing of the string isn't really necessary, especially when you only prompt once.
  • The reason you're printing on a single line is because you're using the print() function rather than the println() function, the difference being that println() appends the newline character at the end.

Next time can you paste code using the code tags with exactly what you have in your editor. It makes it a hell of a lot easier to read and debug.

 

My implementation:

public static void main(String[] args) {	// Set number of players	int numPlayers = 2;	// Create array for player names	String playerNames[] = new String[numPlayers];	// capture input source	Scanner input = new Scanner(System.in);	// For player slots...	for (int i = 0; i < numPlayers; i++) {		// Prompt for name		System.out.println("Enter the name of Player " + Integer.toString(i + 1) + ":");		// Store name		playerNames[i] = input.nextLine();	}	// Output names	System.out.println("Player names:");	// For each player	for (String s:playerNames) {		// Print out name		System.out.println(s);	}}
Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×