0

I have a string wherein I need to split it and store in an ArrayList as seen below; All these using Java7.

The below is the input for my program;

String inputStr = "ABC:DONE:07JAN90:1234:00001111:U:1234567:9632587:4561230:EC:IN:4815263:3621547:0314783)";

What I am trying to achieve here is an ArrayList as seen below after its grouped.

   "List" : [ 
    {
    1234567,
    4815263
    },
    {
     9632587,
    3621547
    },
     {
     4561230,
     0314783
     }
    ]
    

I have a sample code written but that doesn't seem to group the elementB into the list. How can we do the grouping of both the elements into a List.

Sample Code:

    String splitStringNeeded = "ABC:DONE:07JAN90:1352:00009279:U:1234567:9632587:4561230:EC:GB:4815263:3621547:0314783)";
            
    String[] vElements = splitStringNeeded.split(":");
    String[] grpV = Arrays.copyOfRange(vElements, 6,10 );
    String[] grpT = Arrays.copyOfRange(vElements, 12, vElements.length);
        
    List<String> groupedV = Arrays.asList(grpV);
    List<String> groupedT = Arrays.asList(grpT);
    
    for(String splitV : groupedV){
        for(String splitT : groupedT){
        
            System.out.println(splitV);
            System.out.println(splitT);
            break;
        
        }
    }
    

I am getting the below output where elementB does not change but repeats itself. Can someone please guide

"List" : [ 
    {
    1234567,
    4815263
    },
    {
     9632587,
    4815263
    },
     {
     4561230,
     4815263
     }
    ]
    

We can consider even the below inputs:

ABC:DONE:07JAN90:1352:00009279:U:1234567:9632587:0:EC:GB:4815263:3621547

ABC:DONE:07JAN90:1352:00009279:U:1234567:0:0:EC:GB:4815263
2
  • 2
    It's very unclear what you are doing. Certainly the comparison with "0" will never match since that isn't in your input. There is no reason to create new ArrayLists just to immediately overwrite them with the results of calling Arrays.asList. I would suggest you try to debug your code and find out what it is doing. How to debug small programs Commented Jan 26, 2022 at 16:19
  • I have now edited my sample program. I am trying to group them into a list Commented Jan 26, 2022 at 16:23

1 Answer 1

1

I have just modified the nested for loop in your sample code to a single for loop
Of course there are few un-knows such as
Does your Source string always follow same pattern as you have displayed
The length of the lists groupedV and groupedT are they always of same length etc. which is not considered

String splitStringNeeded = "ABC:DONE:07JAN90:1352:00009279:U:1234567:9632587:4561230:EC:GB:4815263:3621547:0314783";

/* Additional code for dynamic allocation of indexes
int indexU = splitStringNeeded.indexOf("U:");
int indexOfGb = splitStringNeeded.indexOf("EC:GB:");
System.out.println("Index of U "+indexU);
System.out.println("Index of GB "+indexOfGb);
String sub1 = splitStringNeeded.substring(indexU+2, indexOfGb-1);
System.out.println("Substring 1 = "+sub1);
String sub2 = splitStringNeeded.substring(indexOfGb+6, splitStringNeeded.length());
System.out.println("Substring 2 = "+sub2);
    //Additional code for dynamic allocation of indexes
*/

String[] vElements = splitStringNeeded.split(":");

String[] grpV = Arrays.copyOfRange(vElements, 6, 9);
String[] grpT = Arrays.copyOfRange(vElements, 11, vElements.length);

List<String> groupedV = Arrays.asList(grpV);
List<String> groupedT = Arrays.asList(grpT);

if(groupedV.size() == groupedT.size()) {
    for(int i=0; i<groupedV.size(); i++){
        System.out.println(groupedV.get(i));
        System.out.println(groupedT.get(i));
    }
}
/*for (String splitV : groupedV) {
    for (String splitT : groupedT) {
        if (!splitV.equals("0")) {
            System.out.println(splitV);
            System.out.println(splitT);
            break;
        }
    }
}*/

I have added the commented code to dynamically pick the index values instead of hard coding like 6,10,12

if you un-comment the first commented block the output will be like below

Index of U 31
Index of GB 57
Substring 1 = 1234567:9632587:4561230
Substring 2 = 4815263:3621547:0314783
1234567
4815263
9632587
3621547
4561230
0314783
1
  • That was the output i was expecting. But yeah the values i have hardcoded for example purpose. there can be values like ABC:DONE:07JAN90:1352:00009279:U:1234567:9632587:0:EC:GB:4815263:3621547 and ABC:DONE:07JAN90:1352:00009279:U:1234567:0:0:EC:GB:4815263 Thats why i have added the check splitV.equals("0") Commented Jan 26, 2022 at 17:16

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.