This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Correct way to use a scanner  RSS feed

 
Ranch Hand
Posts: 109
1
  • Mark post as helpful
  • send pies
  • 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: 12994
217
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Master Rancher
Posts: 3887
45
  • Mark post as helpful
  • send pies
  • 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: 62881
203
  • Mark post as helpful
  • send pies
  • 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: 109
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
excellent will give it a go...thanks
 
Bartender
Posts: 20353
111
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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: 62881
203
  • Mark post as helpful
  • send pies
  • 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.
 
It will give me the powers of the gods. Not bad for a tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!