Hi there,

It's been a long time since I've used delimiters in the Scanner class in Java. If I wanted to have any numbers between an expression, such as xx/xx/xxxx or an infinite amount of numbers between them with just the forward slashes as delimiters, what would the syntax be? I came up with

s.useDelimiter(".*/.*/.*")

And is this similar to C++ and Python? Also can't remember. D'oh! Sorry, and thanks for your help!

Recommended Answers

All 4 Replies

I don't know why I'm rewarding your laziness like this, but here's the code you could have / should have written for yourself in less time than it took to write that post

String input = "12/345/6789";
      Scanner s = new Scanner(input).useDelimiter("/");
      System.out.println(s.nextInt());
      System.out.println(s.nextInt());
      System.out.println(s.nextInt());

All right, well, ignoring the sarcasm, I've tried that as well as other combinations. If I have a text file that is separated as such:

12/32/4323
31/13/23
...

This throws an InputMismatchException because of the whitespace, so isn't it * or .* to fix that and the new lines? Thanks for the help.

That's not the same format as the data in the first post, so onviously the answer is going to be different.
Are you saying there may also be arbitrary white space in the data? Or only inbetween numbers? As well as or instead of the / demiliters?

Assuming they are not embedded then you need to delimlit on any number of white spaces and/or / characters.
You could try useDelimiter("[\\s/]*"
... but there are others here who are for moreexpert in regexs than I am.

Maybe this:
For a delimiter of one or more white space chars OR a "/"
useDelimiter("\\s+|/")

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.