Saturday, November 23, 2024
Google search engine
HomeLanguagesJavaScanner findInLine() method in Java with Examples

Scanner findInLine() method in Java with Examples

findInLine(Pattern pattern)

The findInLine(Pattern pattern) method of java.util.Scanner class tries to find the next occurrence of the specified pattern ignoring delimiters. If the pattern is found before the next line separator, the scanner advances past the input that matched and returns the string that matched the pattern. If no such pattern is detected in the input up to the next line separator, then null is returned and the scanner’s position is unchanged.

Syntax:

public String findInLine(Pattern pattern)

Parameters: The function accepts a mandatory parameter Pattern pattern which is pattern which is scanned for.

Return Value: The function returns the scanner’s delimiting pattern.

Exceptions: The function throws an IllegalStateException if this scanner is closed.

Below programs illustrate the above function:

Program 1:




// Java program to illustrate the
// findInLine() method of Scanner class in Java
  
import java.util.*;
import java.util.regex.Pattern;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Get the string to be searched
            String s = "Geeksforgeeks has Scanner Class Methods";
  
            // Print the string
            System.out.println("Target String:\n" + s);
  
            // create a new scanner
            // with the specified String Object
            Scanner scanner = new Scanner(s);
  
            // finds a pattern of any 5 letter plus "for"
            System.out.println("\nAny 5 letter plus for : "
                               + scanner.findInLine(
                                     Pattern.compile(".....for")));
  
            // close the scanner
            scanner.close();
        }
        catch (IllegalStateException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Target String:
Geeksforgeeks has Scanner Class Methods

Any 5 letter plus for : Geeksfor

Program 2: To demonstrate IllegalStateException




// Java program to illustrate the
// findInLine() method of Scanner class in Java
  
import java.util.*;
import java.util.regex.Pattern;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Get the string to be searched
            String s = "Geeksforgeeks has Scanner Class Methods";
  
            // Print the string
            System.out.println("Target String:\n" + s);
  
            // create a new scanner
            // with the specified String Object
            Scanner scanner = new Scanner(s);
  
            // close the scanner
            scanner.close();
  
            // finds a pattern of any 5 letter plus "for"
            System.out.println("\nAny 5 letter plus for : "
                               + scanner.findInLine(
                                     Pattern.compile(".....for")));
  
            // print the next line of the string
            System.out.println("" + scanner.nextLine());
        }
        catch (IllegalStateException e) {
            System.out.println("Exception thrown: " + e);
        }
    }
}


Output:

Target String:
Geeksforgeeks has Scanner Class Methods
Exception thrown:
 java.lang.IllegalStateException:
 Scanner closed

findInLine(String pattern)

The findInLine(String pattern) method ofjava.util.Scanner class tries to find the next occurrence of a pattern constructed from the specified string pattern, ignoring the delimiters.

Syntax:

public String findInLine(String pattern)

Parameters: The function accepts a mandatory parameter string pattern which is scanned for.

Return Value: The function returns the scanner’s delimiting pattern.

Exceptions: The function throws an IllegalStateException if this scanner is closed.

Below programs illustrate the above function:

Program 1:




// Java program to illustrate the
// findInLine() method of Scanner class in Java
  
import java.util.*;
import java.util.regex.Pattern;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Get the string to be searched
            String s = "Geeksforgeeks has Scanner Class Methods";
  
            // Print the string
            System.out.println("Target String:\n" + s);
  
            // create a new scanner
            // with the specified String Object
            Scanner scanner = new Scanner(s);
  
            // finds a pattern of any 5 letter plus "for"
            System.out.println("\nAny 5 letter plus for : "
                               + scanner.findInLine(
                                     Pattern.compile(".....for")));
  
            // close the scanner
            scanner.close();
        }
  
        catch (IllegalStateException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Target String:
Geeksforgeeks has Scanner Class Methods

Any 5 letter plus for : Geeksfor

Program 2: To demonstrate IllegalStateException




// Java program to illustrate the
// findInLine() method of Scanner class in Java
  
import java.util.*;
import java.util.regex.Pattern;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Get the string to be searched
            String s = "Geeksforgeeks has Scanner Class Methods";
  
            // Print the string
            System.out.println("Target String:\n" + s);
  
            // create a new scanner
            // with the specified String Object
            Scanner scanner = new Scanner(s);
  
            // close the scanner
            scanner.close();
  
            // finds a pattern of any 5 letter plus "for"
            System.out.println("\nAny 5 letter plus for : "
                               + scanner.findInLine(
                                     Pattern.compile(".....for")));
        }
  
        catch (IllegalStateException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}


Output:

Target String:
Geeksforgeeks has Scanner Class Methods
Exception thrown :
 java.lang.IllegalStateException:
 Scanner closed

Reference: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#findInLine(java.util.regex.Pattern)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments