Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Input help  RSS feed

 
Jane Brown
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again.

I have completed this program and both the city and college name are printing out together. I can't see what I have done wrong. Thanks!

Java.PNG
[Thumbnail for Java.PNG]
 
Jhonson Fernando
MyExamCloud Software Support
Ranch Hand
Posts: 692
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use next() instead of nextLine()

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You use nextInt() to get the age, but that method only consumes the characters from the number. The carriage return/new line characters are still in the buffer. So when you call next() for the next question it sees the new line characters and returns you an empty String.

You need to add an extra next() method call after nextInt() but before the next question.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a long but useful FAX on user input in Java.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!