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.

Syntax

Following is the declaration of findInLine() method:

Parameter

DataTypeParameterDescriptionRequired/Optional
StringpatternIt is a string specifying the pattern to search forRequired
PatternpatternIt is the pattern to scan forRequired

Returns

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

Exceptions

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:

Output: World
Left String: !

Example 2

Output:

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

Example 3

Output:

Output: Hello
Left String:  World! Hello India!

Example 4

Output:

Enter your email address: [email protected]
Output:rahul