bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java Scanner findInLine() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java Scanner findInLine() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

// Create a scanner object Scanner myObj = new Scanner("Please send an email to info@example.com ___ more details.");
3Order

Put the learning moves in the order that makes the concept easiest to apply.

Find an email address in a line of text:
Definition and Usage
Java Scanner findInLine() Method

❮ Scanner Methods

Example

Find an email address in a line of text:

// Create a scanner object Scanner myObj = new Scanner("Please send an email to info@example.com for more details.");
// Get the email address with a pattern String email = myObj.findInLine("[a-zA-Z]+@[a-zA-Z]+.[a-zA-Z]{2,}");
// Show the email if found
if (email != null) {
 System.out.println(email);
} else {
System.out.println("No email found");
}
// Close the scanner myObj.close();

Definition and Usage

The findInLine() method searches up to the next line break in the scanner for the first match of a regular expression provided by a Pattern object or a string. If a match is not found then it returns null .

If a match is found the scanner advances to the first character following the match.

Learn more about the regular expressions in our Java RegEx tutorial .

One of the following

public String findInLine(Pattern
pattern )
public String findInLine(String
pattern )

Parameter Values

ParameterDescription
patternRequired. A string or Pattern object. Specifies the regular expression used in the search.

Technical Details

Returns:A String containing the matched text or null if no match was found.
Throws:IllegalStateException - If the scanner has been closed.

❮ Scanner Methods

Previous

Java Scanner delimiter() Method

Next

Java Scanner findWithinHorizon() Method