findInLine( )
findInLine( ) is useful if you want to locate a specific pattern. This method searches for the specified pattern within the next line of text. If the pattern is found, the matching token is consumed and returned. Otherwise, null is returned.
Its general forms are shown here:
String findInLine(Pattern pattern)
String findInLine(String pattern)
/**
*Output:
28
*/
import java.util.Scanner;
public class MainClass {
public static void main(String args[]) {
String instr = "Name: Joe Age: 28 ID: 77";
Scanner conin = new Scanner(instr);
conin.findInLine("Age:"); // find Age
if (conin.hasNext())
System.out.println(conin.next());
else
System.out.println("Error!");
}
}
Home
Java Book
Essential Classes
Java Book
Essential Classes
Scanner:
- Scanner
- Setting Delimiters
- findInLine( )
- findWithinHorizon( )
- skip( )
- new Scanner(FileReader file)
- new Scanner(InputStream source)
- Scanner: hasNext()
- Scanner: hasNextBoolean()
- Scanner: hasNextDouble()
- Scanner: hasNextInt()
- Scanner: hasNextLine()
- Scanner: next()
- Scanner: nextBoolean()
- Scanner: nextDouble()
- Scanner: nextInt()
- Scanner: nextLine()