• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Correct way to use a scanner

 
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi guys

i have a static scanner declared at the top of my program
its a mock invoicing program

i have two methods
1 that questions the user about name sex age etc etc
2 prints out specific data

i have the whole program in a while loop so that at the end you can run it again or quit

the problem im having is that th scanner seems to input data when i call the first method at the end of the program
i use
scanner.next() for single character input
scanner.nextLine() for a name
scanner.nextByte() for age
etc

why is this happening?
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

wayne brandon wrote:
the problem im having is that th scanner seems to input data when i call the first method at the end of the program ...
why is this happening?


Your description is not very clear. Please post the relevant code so we know exactly what you tried.

When switching between scanner methods that read strings and methods that read numbers, it's probably best to call nextLine() after a nextInt() or nextByte() to skip past any newlines in the input stream.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's not just between Strings and numbers, it's between the methods that work with tokens (all the next methods except nextLine) and nextLine, which ignores tokens and simply returns up to (and inclusing) the next new line.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:. . . it's probably best to call nextLine() after a nextInt() or nextByte() to skip past any newlines in the input stream.

DT is right; you may need to clear an empty line before calling newLine(). The problem occurs if you call nextAnythingElse() before newLine(). Many books are not at all clear about newLine().
 
wayne brandon
Ranch Hand
Posts: 289
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
excellent will give it a go...thanks
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Junilu Lacar wrote:. . . it's probably best to call nextLine() after a nextInt() or nextByte() to skip past any newlines in the input stream.

DT is right; you may need to clear an empty line before calling newLine(). The problem occurs if you call nextAnythingElse() before newLine(). Many books are not at all clear about newLine().



newLine(), or nextLine()? I don't see newLine in the Scanner docs.

The nextLine method makes sense if you're into Unix-style config and script files. Or Python. Or C++ (and post-C++ C). Or SQL. All of these languages typically have some sort of token that indicates that the rest of the line is a comment and should be ignored.

For example:


In each of these cases, we have a comment delimiter ("//", "#", ";", "--") which tells the lexical scanner to skip (ignore) the rest of the line. So if you're scanning a line and see a comment delimiter, call nextLine() and you're now reading to scan the next line of input - if there is one.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:. . . newLine(), or nextLine()? I don't see newLine in the Scanner docs. . . . if you're scanning a line and see a comment delimiter, call nextLine() and you're now reading to scan the next line of input - if there is one.

You are right; I wrote that wrongly.
Yes, you can get the first part of the line before the # by using # as your delimiter. next() will give you whatever precedes the # and nextLine() will read the remainder of the line, however short or long it is, and take you to the next line. Whatever was read by next() constitutes the text you want to keep.
 
reply
    Bookmark Topic Watch Topic
  • New Topic