Differences between Java: .nextLine () and .nextDouble ()

I read the API for Java because I had a question about the difference between .nextLine() and .nextDouble() . In the API, this talks about this for .nextLine() :

"Adapts this scanner to the current line and returns the missing input. This method returns the remainder of the current line, with the exception of the line separator at the end. The position is set to the beginning of the next line."

Easy enough ... it skips the current line you're on, and then returns a line that you just skipped. But for .nextDouble() it says the following:

"Scans the next input token as double. This method will throw an InputMismatchException if the next token cannot be converted to a valid double value. If the transfer is successful, the scanner moves past the input file that matches."

Does this mean that .nextDouble() does not jump to a new line and that it reads only to the end of the double and then stops at the end of the line? This will help me understand the program I'm currently working on. Thank you guys and girls!

+4
source share
2 answers
 nextDouble() 

only reads the next 4 bytes from the input stream, which is double and does not go to the next line, while nextLine () does. Also, I think you need to understand that a new line is indicated by the string '\ n', and obviously the number will not contain the string, so 'nextDouble ()' will not โ€œadvanceโ€ to the next line.

EDIT: Razvan is right.

+1
source

It reads the next token (between two separator characters, usually white characters), and not the next 4 bytes. Then it converts this token to double, if possible.

 Ex: Bla bla bla 12231231.2121 bla bla. 

The 4th token can be read using nextDouble (). It reads 13 characters and converts this string to double if possible.

+3
source

All Articles