Java - How to search a string for 6 random numbers
Asked Answered
H

3

5

I have a (large) string file in my application which contains a series of random character [a-Z] and [0-9] but also ";","/", "?", ":" and "@". I would like my application to tell me the nearest position where 6 digits are displayed consecutively ( like "105487" or "558463").

What would be the best way to achieve this? Thank you for looking into this.

Hothouse answered 1/10, 2012 at 21:20 Comment(6)
Please clarify on "nearest position". Is it the first occurance in the file?Slotter
Does the position mean line number and column in the file?Crazed
yes, i meant the first occurance. The position just means the number that the indexOf function in java would give as a number.Hothouse
You can find what you are looking for here: #8938998Plumbism
Are you looking for a sequence of speicific digits or just a sequence of any digits?Necrolatry
I am looking for a sequence of any digits.Hothouse
U
1

An effective approach would be to iterate the characters of the string and test if each is a digit. Upon finding a match continue to look for the rest of the sequence. Something like

int nDigits=0, i = 0;
CharacterIterator it = new StringCharacterIterator("very long string123456");
for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()) {
  i++;
  nDigits = (ch.isDigit() ? nDigits++ : 0);
  if (nDigits == 5) {
      // DONE. Position is "i"
  }
}
Usurer answered 1/10, 2012 at 21:25 Comment(1)
Thank you, I will definitely try this.Hothouse
R
4

You could use regex.

String regex = "(\\d{6})";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(YOU STRING HERE);
// Check all occurrences
while (matcher.find()) {
    System.out.print("Start index: " + matcher.start());
    System.out.print(" End index: " + matcher.end());
    System.out.println(" Found: " + matcher.group());
}

This would do the job.

(Code sample from here)

Resistless answered 1/10, 2012 at 21:28 Comment(5)
for very large string it likely will fail with OOMEStaggs
why not use REGEX \\d{6} instead?Adolescence
Well, I don't know how long is string is, but I think it's a good option. Thanks for pointing it out though.Resistless
@gtgaxiola, good question. I didn't remember it.. Edited. thanks.Resistless
Thank you. I will also try this approach, but I think that the answer that Johan Sjöberg posted would be best suited for my application.Hothouse
H
3

Use Character.isDigit while iterating the string's characters and then count a number up until you have found 6 consecutive numbers or reset it if the sequence breaks. Keep track of the index and you can simply calculate the nearest position by subtraction.

This is not very efficient, but I think O(n) is sufficient if the strings are not too large. For optimization take a look on what Luiggi Mendoza has suggested in the comments.

Headwaters answered 1/10, 2012 at 21:24 Comment(4)
Thank you for your answer. I thought this would be the only solution, but i was hoping that java had a more efficient way of doing this. I will look into this.Hothouse
@user1637234 you could use a regular expression like Lablabla has posted.Tubman
You can optimize this: if you find a digit in position X, then check the character in position X + 5, if that character is not a a digit, then start searching from here. If it's a digit, then go back (X+4, X+3...) until you find a non-digit and start again from the least position.Tubman
@Adolescence yeah, one of my String matching favorite algorithms when I can't rely on RegEx.Tubman
U
1

An effective approach would be to iterate the characters of the string and test if each is a digit. Upon finding a match continue to look for the rest of the sequence. Something like

int nDigits=0, i = 0;
CharacterIterator it = new StringCharacterIterator("very long string123456");
for (char ch=it.first(); ch != CharacterIterator.DONE; ch=it.next()) {
  i++;
  nDigits = (ch.isDigit() ? nDigits++ : 0);
  if (nDigits == 5) {
      // DONE. Position is "i"
  }
}
Usurer answered 1/10, 2012 at 21:25 Comment(1)
Thank you, I will definitely try this.Hothouse

© 2022 - 2024 — McMap. All rights reserved.