Home > Back-end >  Java nextline () method
Java nextline () method

Time:11-30

Have a doubt, next (), nextint (), the two is to read the content of the carriage returns or Spaces before, enter and Spaces will be stranded in the buffer zone, followed by nextline (), leads to directly receive return, receive the end of the nextline () before receiving the return, the content of the return is in the buffer zone, why followed netxline (), does not affect the??? Next (), nextint () method to check the information, it will automatically filter out valid characters before enter invalid characters or Spaces, etc

CodePudding user response:

Written in the document clearly
Public String 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.
Since this method continues to search through the input & for a line separator, it may buffer all of the input the searching for the line to skip the if no line separators are present.
Netxline () will return the data before the newline (not including line breaks), but also move the pointer to the next line, although is also didn't read the newline character, but will skip it directly into the beginning of a line under the

As for the next (), nextint () will automatically filter out valid characters in front of the carriage return or Spaces such as invalid characters, look at the following example
Scanner sc=new Scanner (" ABC \ n \ n \ n \ n \ n \ n 456 123 789 ");
System. The out. Printf (" [% s], [% d], [% s], [% s] \ n ", sc. NextLine (), sc. NextInt (), sc. NextLine (), sc. The next ());
Nextint, next () () will be filtered out in front of the invalid characters, but after reading data will not automatically jump to the next valid character (that is, the back of the blank and return still stranded), and nextLine skips followed by a carriage return

  • Related