Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS 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:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Question about scanner methods

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was solving BJP4 Self-Check 5.23: robustInput on Practice-It and I wrote this piece of code.

It was supposed to work such that the program will not proceed until the user has entered a valid age (any integer), but I found that whenever I entered something other than an integer, the program goes into an infinite loop. Can someone explain to me why this happens?
 
Saloon Keeper
Posts: 8598
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In you body you need to call
to flush out the non-int.
 
Vicky Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't there also a newline character(\n) after my input? If so, shouldn't the result still be an infinite loop because I didn't flush out "\n"?
I thought that maybe the console.next() method also consumes "\n", but "\n" is a delimiter and next() only consume tokens
 
Carey Brown
Saloon Keeper
Posts: 8598
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It depends. If someone enters
ABC 42
Of course you want to throw away ABC, but do you also want to throw away the 42 ?
If so then call nextLine().
 
Vicky Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh I just remembered that hasNextInt() only scans tokens
 
Carey Brown
Saloon Keeper
Posts: 8598
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but to flush out the non-int you could call next() or nextLine(). Different behavior though.
 
Vicky Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry, I also forgot to mention that the problem says "You may assume that the user enters a single token of input each time when prompted"
But that brings me to my next question.
This is what the greater portion of my code looks like:

The int part of my code works no matter if I use next() or nextLine(), but I can only use next() for the double part because when I use nextLine(), this happens:
expected output:
Type your GPA: a
Type your GPA: bcd
Type your GPA: 2.5

my output:
Type your GPA:
Type your GPA: a
Type your GPA: bcd
Type your GPA: 2.5

And so I was wondering why nextLine() doesn't work for the double portion of the code?
 
Carey Brown
Saloon Keeper
Posts: 8598
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 6 will leave a pending new-line. You can call nextLine() immediately after that to flush the new-line.
 
Vicky Lee
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What if my code is (next() for int and nextLine() for double):

Shouldn't a lot of \n's be left over? So then I should have:
Type your age: hello
Type your age: ?
Type your age: 3.14
Type your age: 25
Type your GPA:
Type your GPA:
Type your GPA:
Type your GPA:
Type your GPA: a
Type your GPA: bcd
Type your GPA: 2.5

But my actual output is:
Type your age: hello
Type your age: ?
Type your age: 3.14
Type your age: 25
Type your GPA:
Type your GPA: a
Type your GPA: bcd
Type your GPA: 2.5
age = 25, GPA = 2.5
 
Carey Brown
Saloon Keeper
Posts: 8598
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are struggling with Scanner and how you expect console input to behave. With console input each data item must be followed by pressing the Enter key. Because Scanner was written to also deal with reading text files they are designed with the ability to put multiple values together on one line. Console input must explicitly forbid this, a task the Scanner was not designed to do.

It is also useful to create some helper methods for console input which encapsulates this concept in order to simplify your application code. Here's an example. The first two promptXXX() methods shouldn't be used but are here so that you can compare their behavior to what you are currently doing.

Note that the constant CONSOLE is created once and used everywhere. Only one Scanner built from System.in should ever be created.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only need to flush the new line if you are dealing with input from the console that can have multiple tokens.
For example, asking for an age and then asking for a full name, using nextInt and then nextLine.

If (as in the example above) you use nextInt and then nextDouble then there is no need to use nextLine after successfully reading in a value.
This is because the new line character left in the buffer is thrown away by the nextDouble call before reading the value.
 
Lookout! Runaway whale! Hide behind this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic