Sunday, July 28, 2019

Difference between next() and nextLine()

Hi Folks,

In this post, we will try to see the difference between taking a string as an input by using next() and nextLine() methods.

1. By using next()

Output:



We can see here that, the next() takes only the first word of the sentence. The return type of the next() is String. It finds and returns the next complete token from this scanner. A complete token is preceded and followed by the input that matches the delimiter pattern. 

2. By using nextLine()




Output:

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. 

No comments:

Post a Comment