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

Simple user input from string  RSS feed

 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little frustrated that I completely wrote my whole database program without any problems but I'm getting stumped on a simple in/out.



Something is going on with my keyboard.nextLine(); for my string input value.

It keeps sending aa empty string to my .addMovie class.

Any help or advise will help greatly.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why you have used nextLine after title and price as well??
 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:why you have used nextLine after title and price as well??


I noticed that after I made this post and I removed that second nextLine();

I'm still not sure why this is putting a empty string in my .addMovie class though.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is taking enter as an input so it is giving you empty.
 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tushar Goel wrote:It is taking enter as an input so it is giving you empty.


So, nextLine() doesn't read a whole line of string input when you have to press enter? Never really had a problem with this creating a empty string before.

What would be the most efficient way to read a whole line of string input then?
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find the explanation here, along with a possible solution. If you search my posts for Scanner and nextLine you will find a different solution, but there was a serious error in the first version.
 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You will find the explanation here, along with a possible solution. If you search my posts for Scanner and nextLine you will find a different solution, but there was a serious error in the first version.


Thanks Ritchie for point that out!

However, I read through it a couple of times and I'm starting to think this is a good solution for if I was calling .nextLine() after I did a .nextAnythingElse();

I'm trying to call .nextLine() before I do .nextAnythingElse(); Never really had a issue with calling .nextLine() before doing .nextInt() before.

 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only a problem if you call nextLine after nextAnythingElse. Calling nextLine after nextLine or calling nextLine before anything else does not produce that problem.
 
Jesse Schlicklin
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesse Schlicklin wrote:
Campbell Ritchie wrote:You will find the explanation here, along with a possible solution. If you search my posts for Scanner and nextLine you will find a different solution, but there was a serious error in the first version.


Thanks Ritchie for point that out!

However, I read through it a couple of times and I'm starting to think this is a good solution for if I was calling .nextLine() after I did a .nextAnythingElse();

I'm trying to call .nextLine() before I do .nextAnythingElse(); Never really had a issue with calling .nextLine() before doing .nextInt() before.



I see what's going on and your explanation worked like a charm! I guess you guys couldn't see the rest of my code but I had a keyboard.nextInt(); higher up in my code that was causing the whole issue.

 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a better solution: write yourself a utility class. Start reading here.

[Added later] And we have seen that sort of problem with Scanner before.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!