- Java.util Package Classes
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util Package Extras
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java Scanner findInLine() Method
Description
The Java Scanner findInLine(Pattern pattern) method attempts 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. This method may block waiting for input that matches the pattern.
Declaration
Following is the declaration for java.util.Scanner.findInLine() method
public String findInLine(Pattern pattern)
Parameters
pattern − the pattern to scan for
Return Value
This method returns the text that matched the specified pattern.
Exception
IllegalStateException − if this scanner is closed
Java Scanner findInLine(String pattern) Method
Description
The java Scanner findInLine(String pattern) method attempts to find the next occurrence of a pattern constructed from the specified string, ignoring delimiters.
Declaration
Following is the declaration for java.util.Scanner.findInLine(String pattern) method
public String findInLine(String pattern)
Parameters
pattern − a string specifying the pattern to search for
Return Value
This method returns the text that matched the specified pattern.
Exception
IllegalStateException − if this scanner is closed
Finding a pattern within the Line Using Scanner Example
The following example shows the usage of Java Scanner findInLine(Pattern pattern) method to find the next occurrence of pattern in a given string. We've created a scanner object using a given string. We've printed the word containing a given pattern using findInLine() method. Then we printed the string using scanner.nextLine() method.
package com.tutorialspoint; import java.util.Scanner; import java.util.regex.Pattern; public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6"; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); // find a pattern of any letter plus "ello" System.out.println(scanner.findInLine(Pattern.compile(".ello"))); // print the next line of the string System.out.println(scanner.nextLine()); // close the scanner scanner.close(); } }
Output
Let us compile and run the above program, this will produce the following result −
Hello World! 3 + 3.0 = 6
Finding a String within the Line Using Scanner Example
The following example shows the usage of Java Scanner findInLine(String pattern) method to find the next occurrence of pattern in a given string. We've created a scanner object using a given string. We've printed the word containing a given pattern using findInLine() method. Then we printed the string using scanner.nextLine() method.
package com.tutorialspoint; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { String s = "Hello World! 3 + 3.0 = 6"; // create a new scanner with the specified String Object Scanner scanner = new Scanner(s); // find a string "World" System.out.println(scanner.findInLine("World")); // print the next line of the string System.out.println(scanner.nextLine()); // close the scanner scanner.close(); } }
Output
Let us compile and run the above program, this will produce the following result −
World ! 3 + 3.0 = 6
Finding a pattern with the Line Using Scanner on User Input Example
The following example shows the usage of Java Scanner findInLine(String pattern) method to find the next occurrence of matched result of pattern in an input. We've created a scanner object using System.in class. We checked the word World using findInLine(String) method.
package com.tutorialspoint; import java.util.Scanner; public class ScannerDemo { public static void main(String[] args) { // create a new scanner with the System input Scanner scanner = new Scanner(System.in); // find a string "World" System.out.println(scanner.findInLine("World")); // print the next line of the string System.out.println(scanner.nextLine()); // close the scanner scanner.close(); } }
Output
Let us compile and run the above program, this will produce the following result − (where we've entered Hello World and pressed enter key.)
Hello World World
To Continue Learning Please Login
Login with Google