'Get first 200 String ,set sequence and increment it if the string is more than 200
I am trying to solve this usecase where I have below json (NOtepadOverflow Data)
"notepadOverflowData": [
{
"sectionSequenceNumber": "01",
"sectionText": "ABCDEFG,HIJKLMNOPQRSTAB,CDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRS1 nationality +"
},
{
"sectionSequenceNumber": "02",
"sectionText": "ABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRS1 nationality +"
} ,
{
"sectionSequenceNumber": "99",
"sectionText": "ABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRSTABCDEFGHIJKLMNOPQRS1 nationality +"
}
],
In this, I need to get first 200 characters and set it in sectionText, and in sectionSequenceNumber it should start from "01". now If the mainString is > 200 , cut it and set that secondSubstring in second block - sectionText(will be next 200) and sectionSequenceNumber = "02" , so on and so forth.
I have tried below things but it is not giving expected result -
String nationality ="01,789,115,243,829,565,371,182,647,4435910257262248077394052327956158518390755253526164992046808288158147741133875811175906263458023936812929164522298298102045746231643365172017152941946079512878050984287357708374166217380767662303239772860984374849481114923522772038584914212671591311297:001,789,115,243,829,565,371,182,647,443,591,025,726,224,807,739,405,232,795,615,851,839,075,525,352,616,499,204,680,828,815,814,774,113,387,581,117,590,626,345,802,393,842873577083741662173807676623032397728609843748494811149235227720385849142126715913112839,075,525,352,616,499,204,680,828,815,814,774,113,387,581,117,590,626,345,802,393,842873577083741662173807676623032397728609843748494811149235227720385849142126715913112";
String nationalityTrimName = nationality.substring(0, Math.min(nationality.length(), 200));
String lastStr = nationality.substring(countIncrement, Math.min(nationality.length(), nationality.length() - 1));
do {
notepadOverflowData.setSectionSequenceNumber(count);//1
notepadOverflowData.setSectionText(nationalityTrimName);
notepadOverflowDataList.add(notepadOverflowData);
while (lastStr.length() >= 200) {
if (lastStr.length() >= 200) {
lastStr = lastStr.substring(countIncrement, Math.min(lastStr.length(), lastStr.length() - 1));
counter = Integer.parseInt(count);
Integer i = counter + 1;
sequenceNumber = String.format("%02d", i);
buildNotepadEntryReq(notepadOverflowDataList, notepadOverflowData1,
sequenceNumber,
lastStr, countIncrement);
}else {
buildNotepadEntryReq(notepadOverflowDataList, notepadOverflowData1, sequenceNumber,
lastStr,countIncrement);
}
if(!StringUtils.isEmpty(lastStr) && lastStr.length() > 200) {
countIncrement = countIncrement + 200;
cnt = Integer.parseInt(sequenceNumber);
Integer i = cnt+1;
sequenceNumber = String.format("%02d",i);
}
}
/*char last = lastStr.charAt(lastStr.length() - 1);
lastStr = lastStr.substring(200, Math.min(lastStr.length(), last));*/
notepadOverflowDataList.add(notepadOverflowData1);
} while (nationalityTrimName.length() > 200);
}
private void buildNotepadEntryReq(List<NotepadOverflowData> notepadOverflowDataList,
NotepadOverflowData notepadOverflowData1, String sequenceNumber,
String lastSecondStr,int countIncrement) {
if (sequenceNumber.length() <= 99) {
notepadOverflowData1.setSectionSequenceNumber(sequenceNumber);
} else {
logger.error("Sequence Number is more than 99");
}
notepadOverflowData1.setSectionText(lastSecondStr);
notepadOverflowDataList.add(notepadOverflowData1);
}
I am getting output as -
"notepadOverflowData": [
{
"sectionSequenceNumber": "01",
"sectionText": "Customer additional Nationalities :01,789,115,243,829,565,371,182,647,4435910257262248077394052327956158518390755253526164992046808288158147741133875811175906263458023936812929164522298298102045746231"
},
{
"sectionSequenceNumber": "03",
"sectionText": "99,204,680,828,815,814,774,113,387,581,117,590,626,345,802,393,842873577083741662173807676623032397728609843748494811149235227720385849142126715913"
},
{
"sectionSequenceNumber": "03",
"sectionText": "99,204,680,828,815,814,774,113,387,581,117,590,626,345,802,393,842873577083741662173807676623032397728609843748494811149235227720385849142126715913"
},
{
"sectionSequenceNumber": "03",
"sectionText": "99,204,680,828,815,814,774,113,387,581,117,590,626,345,802,393,842873577083741662173807676623032397728609843748494811149235227720385849142126715913"
},
Please let me know what i am missing here ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
