To find within the next count characters : Scanner « File « Java Tutorial






  1. String findWithinHorizon(Pattern pattern, int count)
  2. String findWithinHorizon (String pattern, int count)

If successful, it returns the matching pattern. Otherwise, it returns null.

If count is zero, then all input is searched until either a match is found or the end of input is encountered.

import java.util.Scanner;

public class MainClass {
  public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("ID:",100);

    if (sc.hasNext())
      System.out.println(sc.next());
    else
      System.out.println("Error!");
  }
}
77
import java.util.Scanner;

public class MainClass {
  public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("ID:",0);

    if (sc.hasNext())
      System.out.println(sc.next());
    else
      System.out.println("Error!");
  }
}
77
import java.util.Scanner;

public class MainClass {
  public static void main(String args[]) {
    Scanner sc = new Scanner("Name: Tom Age: 28 ID: 77");

    sc.findWithinHorizon("IDs:",0);

    if (sc.hasNext())
      System.out.println(sc.next());
    else
      System.out.println("Error!");
  }
}
Name:








11.54.Scanner
11.54.1.Using Scanner to receive user input
11.54.2.Using Scanner: the complement of Formatter
11.54.3.In general, to use Scanner, follow this procedure
11.54.4.Creating a Scanner: read from standard input: Scanner conin = new Scanner(System.in)
11.54.5.Creating a Scanner to read from a string
11.54.6.Using Scanner to read several different unknown types of data
11.54.7.Setting Delimiters for Scanner
11.54.8.To obtain the current delimiter pattern: Pattern delimiter( )
11.54.9.Searching for the specified pattern within the next line of text
11.54.10.To find within the next count characters