Javatpoint Logo
Javatpoint Logo

Java Scanner findInLine() Method

The findInLine() is a Java Scanner class method which is used to find the next occurance of a pattern. There is two different types of Java findInLine() method which can be differentiated depending on its parameter. These are:

  1. Java Scanner findInLine(String pattern) Method
  2. Java Scanner findInLine(Pattern pattern) Method

findInLine(String pattern) Method:

This is a Java Scanner class method which is used to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.

findInLine(Pattern pattern) Method:

This is an inbuilt method of Java Scanner class which is used to find the next occurrence of the specified pattern ignoring delimiters.


Following is the declaration of findInLine() method:


DataType Parameter Description Required/Optional
String pattern It is a string specifying the pattern to search for Required
Pattern pattern It is the pattern to scan for Required


The findInLine() method returns the text that matched the specified pattern.


IllegalStateException- This method throws exception if the innvocation is done after the scanner has been closed.

Compatibility Version

Java 1.5 and above

Example 1


Output: World
Left String: !

Example 2


Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.base/java.util.Scanner.ensureOpen(
	at java.base/java.util.Scanner.findInLine(
	at java.base/java.util.Scanner.findInLine(
	at myPackage.ScannerFindInLineExample2.main(

Example 3


Output: Hello
Left String:  World! Hello India!

Example 4


Enter your email address: [email protected]

Next TopicJava Scanner Class

Help Others, Please Share

facebook twitter pinterest