java.util.Scanner findInLine(Pattern pattern)

Description :

This java tutorial shows how to use the findInLine(Pattern pattern) method of Scanner class of java.util package. This method returns a String object that satisfies the Pattern object specified as method argument.

Method Syntax :

public String findInLine(Pattern pattern)

Parameter Input :

 

DataType Parameter Description
N/A N/A This method argument tells the Scanner on which Pattern object to search

 

Method Returns :

This method simply returns the string that satisfies the pattern object specified on the method argument.

Compatibility Version :

Requires Java 1.5 and up

Exception :

IllegalStateException

– The IllegalStateException will be thrown by findInLine method of Scanner class if the invocation is done after the scanner has been closed.

Discussion :

The Scanner findInLine(Pattern pattern) method is used to search for a pattern on the scanner buffer regardless of delimiters. This is the same as calling the method findInLine(String pattern).

Java Code Example :

This java example source code demonstrates the use of findInLine method of Scanner class. Basically this codes make use of hasNextLine() and nextLine() to move through the contents of the file line by line. And by using the findInLine() method, we have used used it to get only the contents which satisfies the Pattern object that we have specified. This code was intelligently designed to extract useful strings only on the file contents.

package com.teknoscope.java.tutorial.scanner;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;

/*
 * Example java source code on the usage of findInLine(Pattern pattern) method
 * of Scanner class to search a specific pattern on the scanner buffer
 */

public class ScannerFindInLinePattern {

	public static void main(String[] args) throws FileNotFoundException {
		// Declare File object
		File file = new File("E:/tmp/java/tutorial/scanner/example.txt");
		// initialize the scanner
		Scanner scan = new Scanner(file);
		// declare the pattern to be used
		Pattern pattern = Pattern.compile("[A-Za-z].*[A-Za-z]");
		// iterate through the file line by line
		while(scan.hasNextLine()){
			// scan for names on the content of the file
				String str = scan.findInLine(pattern);
				if(str != null){
					// print the string content that satisfies the pattern object
					// specified as method argument
					System.out.print(str+" ");
				}
			// advance to the next line
			scan.nextLine();
		}
		// close the scanner object;
		scan.close();

	}
}

File Contents:

****************
*Java tutorial*
* that makes use of*
* findInLine() method
* of String class*
****************

Sample Output :

Running the findInLine(Pattern pattern) method example source code of Scanner class will give you the following output

Java tutorial that makes use of findInLine() method of String class

Exception Scenario :

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
	at java.util.Scanner.ensureOpen(Unknown Source)
	at java.util.Scanner.findInLine(Unknown Source)
	at com.teknoscope.java.tutorial.scanner.ScannerFindInLinePattern.main(ScannerFindInLinePattern.java:36)

Similar Method :

  • N/A

Suggested Reading List :

References :