• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Simple user input from string

 
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why you have used nextLine after title and price as well??
 
Jesse Schlicklin
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 954
4
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is taking enter as an input so it is giving you empty.
 
Jesse Schlicklin
Greenhorn
Posts: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 76825
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76825
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 29
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76825
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You'll never get away with this you overconfident blob! The most you will ever get is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic