5 Replies - 94 Views - Last Post: 04 February 2012 - 08:38 PM Rate Topic: -----

Topic Sponsor:

#1 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 06-January 12

interactive input error

Posted 04 February 2012 - 06:54 PM

So, im making a game and my program asks the user "how many people are playing?" and then depending on the input, the is suppose to ask for the first players name, then user enter name, then the program asks for the 2nd persons name, and so on...
but when my current code will ask the number of players then just print out the questions and not take any responses from the user and i get a run-time error then..

System.out.println("Enter number of players: ");
		numPlayer = in.nextInt();
		
		for(int i = 1;i < (numPlayer+1);i++)
		{
			System.out.println("Enter name for Player "+i+": ");
			String name = in.nextLine();
			players.add(name);
		}



Is This A Good Question/Topic? 0
  • +

Replies To: interactive input error

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,072
  • Joined: 05-April 11

Re: interactive input error

Posted 04 February 2012 - 07:03 PM

Please post your full code and copy past the exact error message you get.

I don't see the error in the code you have posted so far
Was This Post Helpful? 0
  • +
  • -

#3 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 06-January 12

Re: interactive input error

Posted 04 February 2012 - 07:14 PM

Enter filename for questions and answers:
lab3.txt
Enter number of players:
2
Enter name for Player 1:
Enter name for Player 2:
greg

thats the console(it wont let me enter in 2 names)
the error is in this part

public static void getPlayerData(Scanner in, int numPlayer, ArrayList<String> players) throws IOException
	{

		System.out.println("Enter number of players: ");
		numPlayer = in.nextInt();
		
		for(int i = 1;i < (numPlayer+1);i++)
		{
			System.out.println("Enter name for Player "+i+": ");
			String name = in.nextLine();
			players.add(name);
		}
	}


Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: interactive input error

Posted 04 February 2012 - 08:01 PM

This is because you mixed nextInt() and nextLine(). The nextInt() scans the next token as integer but does not advance past the current line. So if nextLine() comes after it, it reads the line feed remain as its next token(because it accept empty line).
You need to clear buffer before calling nextLine() every time you use nextInt(), next(),nextDouble()... by calling nextLine() first before calling to take input. So in your code before starting the loop you will add:
in.nextLine(); 
for(int i = 1;i < (numPlayer+1);i++)

Or to avoid it at all, some people use to use only nextLine() for the whole input and then parse the input according to its data type required
Was This Post Helpful? 1
  • +
  • -

#5 GDubz  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 87
  • Joined: 06-January 12

Re: interactive input error

Posted 04 February 2012 - 08:26 PM

ahhh ok, that worked, thanks buddy
Was This Post Helpful? 0
  • +
  • -

#6 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: interactive input error

Posted 04 February 2012 - 08:38 PM

Glad we could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1