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
"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