0

I am trying to extract a substring of a specified index from a line for each loop. Here is the string:

String allPins = "16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472
              16101702000000 376627817243
              16101702000001 326520924472"; 

It is not an array of type string. The first 14 digits is the batch number. I want extract the second 12-digit number, i.e. pin number as a substring everytime I loop through the string.

here is my code so far:

for (int i = 0; i < allPins.length();i++){

      String str = allPins.substring((28*i)+15,(28*i)+28);
      String test  = str;     

      System.out.println(test);  
}

//I am getting an outOfBoundError.

//Please help me out on how to best extract the substrings.

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
Latz
  • 31
  • 1
  • 8

6 Answers6

0

As others have already commented, you might have an easier time if you just split the string by whitespace. In this case, I would split on \s+, meaning one or more whitespace characters.

String[] parts = allPins[i].split("\\s+");
for (int i=1; i < parts.length; i=i+2) {     // iterate and print odds only
    System.out.println(parts[i]);
}

This approach should be fine if you know that your data is well-formed, namely that each 12 digit PIN is always preceded by a 14 digit batch number (and only preceded by that).

If you have irregular data, which your question does not imply, then you might have to go with a matcher.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0
String str = allPins.substring((28*i)+15,(28*i)+28);

The above line is the issue, when i value increases it leads to out of bound error, because you are going more than the defined index of the string.

The better way is to loop and split by spaces and check...

String str = allPins.substring(allPins.split(" ")[1].toString());
AurA
  • 12,135
  • 7
  • 46
  • 63
  • Thanks AurA, I tried going with your correction but I am getting an imcompatible types error: String cannot be converted to int. //[1] – Latz Jan 09 '17 at 13:50
0

I want extract the second 12-digit number, i.e. pin number as a substring everytime I loop through the string.

You don't even need to use the substring method. See the following sample:

  for (String num: allPins.split(" ")) {
     if(num.length() == 12)
         System.out.println(num);
  }
VHS
  • 9,534
  • 3
  • 19
  • 43
  • hi VHS, thanks for you help. I'm a confused where in your code is the extracted pin as substring. I am sure num.length()==12 is a boolean. – Latz Jan 09 '17 at 14:38
  • @Latz, you are welcome. Regarding your other question, the `split` gives you all the numbers separated by a space. However you have two types of numbers - one with 16 digit length and another with 12. The latter is your pin number. – VHS Jan 09 '17 at 16:23
0

Yes you can try like @Kevin Esche suggested, please try splitting the string based on whitespace. This should help you.

Try with the below code snippet:

for (int i = 0; i < allPins.length();i++){

  String[] str = allPins.split("\\s");
  String test  = str[1];     

  System.out.println(test);  

}

  • Thanks Neelmani for sharing some light on this, I did as you said but I still cannot print all the substrings. I think I am having difficulty in place 'i' for the iteration to the next line. – Latz Jan 09 '17 at 14:42
0

Splitting the string will be a better way of doing this.

Try

String[] strArr = allPins.split(" ");

More of white space splitting

Now you have an array of strings with your batch number and pin number adjacently. You can access them by iterating through a for loop.

Also keep in mind to do

string.equals("checking string")

instead of

string== string2

to check the equality.

Community
  • 1
  • 1
Gayan Kavirathne
  • 2,909
  • 2
  • 18
  • 26
0

String[] s1 = allPins.split(" "); System.out.println(s1.length);

for (int i = 0; i < s1.length/2 -1;i++){

String str = allPins.substring((28*i)+15,(28*i)+28);
String test  = str;     

System.out.println(test);  

}

  • Thanks ashwani, appreciate it. But the result is only printing half of the total number of required substrings. – Latz Jan 09 '17 at 14:46