This week's giveaway is in the Cloud/Virtualization forum. We're giving away four copies of Production-Ready Serverless (Operational Best Practices) and have Yan Cui on-line! See this thread for details.
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
scanner.next() for single character input
scanner.nextLine() for a name
scanner.nextByte() for age
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.
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.
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.
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.
When it comes to destroying a civilization, gas chambers cannot hold a candle to echo chambers.
posted 2 months ago
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.
I love a woman who dresses in stainless steel ... and carries tiny ads:
global solutions you can do in your home or backyard