Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
547 views
in Technique[技术] by (71.8m points)

methods - Java: .nextLine() and .nextDouble() differences

I was reading the API for Java because I had a question on the difference between .nextLine() and .nextDouble(). In the API, it says this for .nextLine():

"Advances this scanner past the current line and returns the input that was skipped. This method returns the rest of the current line, excluding any line separator at the end. The position is set to the beginning of the next line."

Easy enough...it skips the current line you are on and then returns the line you just skipped. But for .nextDouble(), it says this:

"Scans the next token of the input as a double. This method will throw InputMismatchException if the next token cannot be translated into a valid double value. If the translation is successful, the scanner advances past the input that matched."

So does this mean that .nextDouble() does not skip to a new line and that it only reads until the end of the double and then stops at the end of the line? This would help me in understanding a program I'm working on now. Thanks guys and gals!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It reads the next token (between 2 separating characters,white characters usually), not the next 4 bytes. It then transforms that token to a double if it can.

Ex: Bla bla bla 12231231.2121 bla bla.

The 4th token can be read using nextDouble(). It reads 13 chars and it transforms that string into a double if possible.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...