0

I am new to the scanner method. Is there a way to check if the scanner.nextLine() contains a pattern?

For example xxx. xxxx: xxxx. xxxx:

So I want to check that there is a . : . : ignoring the xxxx in between. The xxxx can represent any primitive type of String.

Any help would be greatly appreciated.

2
  • I usually use Scanner for simple parsing of lines and use regex / String#split(...) for more detailed parsing. Myself, I'd just get the line with the Scanner and then split the String. Commented Apr 25, 2017 at 0:24
  • Thanks for the response. Its more to check then for splitting purposes. Commented Apr 25, 2017 at 0:34

1 Answer 1

0

I would use the findInLine 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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.