7 Replies - 324 Views - Last Post: 17 August 2012 - 12:00 PM Rate Topic: -----

#1 MeeechiganMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-June 12

Glitch in my program

Posted 17 August 2012 - 10:57 AM

This is a program I made to help me with arrays. The program asks you how many players do you have. Then it will ask you to enter then name of player one, two, three, etc. When I enter a name such as "Alex" the program runs smoothly, but if I enter a nmae such as "Alex Avila" for some reason my program will spit out a response such as:
"How many players do you have? 9
Enter the name of player 1: Alex Avila
Enter the name of player 2: Enter the name of player 3:"
Any help would be greatly appreciated.

import java.util.Scanner;		//Needed for Scanner class

public class ArrayDemoPlayer
	{
		public static void main (String [] args)
		{
			int numPlayers;				//Number of players
			String[] players;				//Array for players
			
			//Create Scanner class
			Scanner keyboard = new Scanner(System.in);
			
			//Get the number of players
			System.out.print("How many players do you have? ");
			numPlayers = keyboard.nextInt();
			
			//Create an array to hold that number of players
			players = new String[numPlayers];
			
			//Get the individual player names
			for (int index = 0; index < players.length; index++)
            {
                System.out.print( "Enter the name of player " + (index+ 1) +  ": ");
                players[index] = keyboard.next();
            }
			
			//Display the players names
			System.out.println();
			System.out.println("Here are the players you've entered: ");
			for ( int index = 0; index < players.length; index++)
				System.out.print(players[index] + " ");
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Glitch in my program

#2 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 207
  • Joined: 13-October 10

Re: Glitch in my program

Posted 17 August 2012 - 11:01 AM

Try using the Scanner.nextLine() function instead of Scanner.next(). (In the loop)

Now it is reading only one word.
The first time Alex and for the second player Avila.

If it reads the line completely it sees the line as one String.
Was This Post Helpful? 0
  • +
  • -

#3 MeeechiganMan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 28-June 12

Re: Glitch in my program

Posted 17 August 2012 - 11:05 AM

View PostSinned, on 17 August 2012 - 11:01 AM, said:

Try using the Scanner.nextLine() function instead of Scanner.next(). (In the loop)

Now it is reading only one word.
The first time Alex and for the second player Avila.

If it reads the line completely it sees the line as one String.


I've tried that, and when I start my program and type in a number for players this is what I get.

"How many players do you have? 9
Enter the name of player 1: Enter the name of player 2:"
Was This Post Helpful? 0
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: Glitch in my program

Posted 17 August 2012 - 11:05 AM

The default delimiter of the Scanner is a whitespace or a line breaking character I think :)

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace

This post has been edited by CasiOo: 17 August 2012 - 11:07 AM

Was This Post Helpful? 0
  • +
  • -

#5 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 165
  • Joined: 22-November 08

Re: Glitch in my program

Posted 17 August 2012 - 11:33 AM

This works, for some reason in java when you use nextLine() you have to manually bump it to the next line the first time through, its a flaw in my opinion, but here's the work around.
for (int index = 0; index < players.length; index++)
{
                             
	System.out.print( "Enter the name of player " + (index+ 1) +  ": ");
	players[index] = keybaord.nextLine();
                             if(index == 0){
                                 players[index] = keybaord.nextLine();
                             }
}



Here's the fix, he double posted and the other post got moved.
Was This Post Helpful? 0
  • +
  • -

#6 Sinned  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 207
  • Joined: 13-October 10

Re: Glitch in my program

Posted 17 August 2012 - 11:42 AM

It is not that you have to bump to the next line the first time, but it is when you read a integer there is still a line break on the stream.
This have to be removed after the integer is read.

adolf625's code should work, but I think it's a bit inefficient.
Because only the stream has to be flushed (after the integer and) before reading new lines.
My version:
keyboard.nextLine(); // flush stream
for (int index = 0; index < players.length; index++)
{
	System.out.print( "Enter the name of player " + (index+ 1) +  ": ");
	players[index] = keyboard.nextLine();
}

Was This Post Helpful? 0
  • +
  • -

#7 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1423
  • View blog
  • Posts: 3,165
  • Joined: 05-April 11

Re: Glitch in my program

Posted 17 August 2012 - 11:55 AM

It isn't a flaw, and nextLine() doesn't flush the stream. nextLine() searches for the next line separator and moves to the beginning of the next line.

Please note that the default delimiter is a whitespace. When you call nextInt() it is still on the same line.

I believe this is how your input will look like:
9\nAlex Avila\n

So you will have the tokens 9, Alex, Avila. When you call nextLine() after you have read the int 9, it will find the \n and move to the next line.
Was This Post Helpful? 0
  • +
  • -

#8 adolf625  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 21
  • View blog
  • Posts: 165
  • Joined: 22-November 08

Re: Glitch in my program

Posted 17 August 2012 - 12:00 PM

Ok let me reword that, I don't necessarily mean glitch, I mean just a crappy way of doing it, it could be easier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1