How to take String input in JavaJava nextLine() methodThe nextLine() method of Scanner class is used to take a string from the user. It is defined in java.util.Scanner class. The nextLine() method reads the text until the end of the line. After reading the line, it throws the cursor to the next line. The signature of the method is: The method returns the line that was skipped. It does not accept any parameter. When it does not find any line, then it throws NoSuchElementException. It also throws IllegalStateException if the scanner is closed. Example of nextLine() method Output: ![]() Java next() methodJava next() method can read the input before the space id found. It cannot read two words separated by space. It retains the cursor in the same line after reading the input. The signature of the method is: The method returns the next complete token from this scanner. It does not accept any parameter. It throws NoSuchElementException if no more tokens are available. It also throws IllegalStateException if the scanner is closed. Example of next() method Output: ![]() We can see that whatever we write after Java is skipped by the method and read only the word Java.
Next TopicJava Tutorial
|
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week