5 Replies - 7525 Views - Last Post: 09 January 2010 - 08:55 AM Rate Topic: -----

#1 oceanus  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 05-January 10

Scanner class won't let me input string?

Posted 09 January 2010 - 08:26 AM

Ok, so basically I have to enter studentID, name, and GPA into an array using the scanner class, but it skips the name...?

 public void init()
	  {
		 Scanner input = new Scanner(System.in);
		 
		 for(int i = 0; i < StudentBody.length; i++)
		 {
			System.out.println("Please enter the ID of student number " + i + ": ");
			StudentBody[i].studentID = input.nextInt();
			System.out.println("Please enter the name of student number " + i + ": ");
			StudentBody[i].name = input.nextLine();
			System.out.println("Please enter the GPA of student number " + i + ": ");
			StudentBody[i].GPA = input.nextDouble();
		 }
	  }



so basically, when I run that, heres wut happens:

Please enter the ID of student number 0:
1 //I entered that
Please enter the name of student number 0: //It skipped that.. wont let me enter it
Please enter the GPA of student number 0:
3 //I entered that
Why is it skipping the name?

Is This A Good Question/Topic? 1
  • +

Replies To: Scanner class won't let me input string?

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Scanner class won't let me input string?

Posted 09 January 2010 - 08:32 AM

Because when you type a number, you put the number in the buffer, as well as a newline character from when you hit enter.
The nextInt() method takes only the the integer out of the buffer, and leaves the newline in it.
Therefore, on your second line, where you prompt for the student's name, nextLine() eats up that remaining newline, and accepts that as it's input.

You can fix this by using multiple Scanners (which is obviously very inefficient) or adding a line input.nextLine() to get rid of anything left in the buffer right after StudentBody[i].studentID = input.nextInt();
Was This Post Helpful? 1
  • +
  • -

#3 oceanus  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 05-January 10

Re: Scanner class won't let me input string?

Posted 09 January 2010 - 08:33 AM

View Postoceanus, on 9 Jan, 2010 - 07:26 AM, said:

Ok, so basically I have to enter studentID, name, and GPA into an array using the scanner class, but it skips the name...?

 public void init()
	  {
		 Scanner input = new Scanner(System.in);
		 
		 for(int i = 0; i < StudentBody.length; i++)
		 {
			System.out.println("Please enter the ID of student number " + i + ": ");
			StudentBody[i].studentID = input.nextInt();
			System.out.println("Please enter the name of student number " + i + ": ");
			StudentBody[i].name = input.nextLine();
			System.out.println("Please enter the GPA of student number " + i + ": ");
			StudentBody[i].GPA = input.nextDouble();
		 }
	  }





so basically, when I run that, heres wut happens:

Please enter the ID of student number 0:
1 //I entered that
Please enter the name of student number 0: //It skipped that.. wont let me enter it
Please enter the GPA of student number 0:
3 //I entered that
Why is it skipping the name?


helppppppppppppp
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is online

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Scanner class won't let me input string?

Posted 09 January 2010 - 08:39 AM

I believe Erik.Price already gave you great advise, I heed you to listen.
Was This Post Helpful? 0
  • +
  • -

#5 chili5  Icon User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,144
  • Joined: 28-December 07

Re: Scanner class won't let me input string?

Posted 09 January 2010 - 08:49 AM

Put nextLine() after your nextInt() method to lose the newline character then you can read the name. Erik. gave you the solution above and you just skipped over it.

Basically nextInt leaves the carriage return in the input buffer and you have to use nextLine to skip the carriage return before you can read in the entire line.
Was This Post Helpful? 0
  • +
  • -

#6 oceanus  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 05-January 10

Re: Scanner class won't let me input string?

Posted 09 January 2010 - 08:55 AM

View Postchili5, on 9 Jan, 2010 - 07:49 AM, said:

Put nextLine() after your nextInt() method to lose the newline character then you can read the name. Erik. gave you the solution above and you just skipped over it.

Basically nextInt leaves the carriage return in the input buffer and you have to use nextLine to skip the carriage return before you can read in the entire line.


thanks erik, that worked, can you guys also help me with my return statement problem? I started a new thread for that, thx in advance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1