Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Reading values from the keyboard

 
Greenhorn
Posts: 4
Java ME Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hi all,

Can some one please explain why we are not able to read second string in this program.


Note: If I uncomment line num 9 it works fine !! can you please give some inputs on this.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Omkant Parashetty wrote:Note: If I uncomment line num 9 it works fine !! can you please give some inputs on this.


I'm probably the wrong person to answer this, since I dislike Scanner intensely; but it has something to do with "consuming the newline".

I presume that after you enter each string, you press the ENTER key. Well (I think), the first nextLine() consumes the newline for the first string you entered, and the second one reads the second string AND the newline, leaving the Scanner ready to read the third string.

Personally, I find it all very counter-intuitive - and actually contrary to the docs for next() (but maybe I'm misinterpreting them) - which is why I generally just use nextLine() to get stuff. But, as I say, I'm not a big fan of Scanner.

I'm sure Campbell will be along to correct me if I'm wrong. He usually is...

Winston
 
Omkant Parashetty
Greenhorn
Posts: 4
Java ME Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Campbell,

Could you please add your inputs here

Thanks in advance,
Omkanth
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Working through https://coderanch.com/how-to/java/UserInput should also be helpful.
 
Marshal
Posts: 74634
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry it has taken me so long to find this thread

Winston is right; it has to do with the line end characters. Look what it says in your book about nextLine and tell us. I am 99% sure it will be wrong.
You can find the explanation here. The simplest answer is to call nextLine() twice, discarding the first return value.That depends on your pushing the enter key after entering the number and will go horrible wrong if you try to enter all this as one line:-

123 Campbell Ritchie

You can write a loop which tests the length of the line, and there is an example here. You probably ought to create a utility class for that sort of method, so you can write


Beware: You must read the whole of the thread after the second link because there is a dangerous mistake somewhere.
 
Ranch Hand
Posts: 57
Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Omkant Parashetty wrote:
Can some one please explain why we are not able to read second string in this program.


Your first next() method returns the next token that is delimited by whitespace (default). If your first input is "A B C<cr>", then s1 is assigned "A" and "B C<cr>" is still in the input buffer.

When nextLine() executes, it takes the remaining line (up to the line separator) from the input buffer and assigns it to s2. So s2 has " B C". The nextLine() method additionally consumes the trailing line separator and advances the position to the beginning of the next line. Note that the user does not have the opportunity to input a value for s2 because the Scanner must consume the leftover characters in the input buffer first.

When the next next() method executes, it again returns the next token delimited by whitespace. There are no characters in the input buffer and the position is on the next line. If your input is again "A B C<cr>", "A" is assigned to s3. "B C<cr>" is again left in the input buffer.

The final next() statement assigns the next token "B" to decider. "C<cr>" is left in the buffer.

Hope this helps.

 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic