I want split lines into list based on line starter ,am getting exception while trying that.
File Content :
H1|!!!!!!!!!!!!!!!!!
L1|DDDDDDDDDDDDD
L2|DDDDDDDDDDDDD
H2|!!!!!!!!!!!!!!!!!
L1|DDDDDDDDDDDDD
L2|DDDDDDDDDDDDD
L3|DDDDDDDDDDDDD
EOF
Output :
Postring size :8
SSSSS:H1|!!!!!!!!!!!!!!!!!
0head:H1|!!!!!!!!!!!!!!!!!
1detail:L1|DDDDDDDDDDDDD
2detail:L2|DDDDDDDDDDDDD
3detail:H2|!!!!!!!!!!!!!!!!!
SSSSS:H2|!!!!!!!!!!!!!!!!!
3head:H2|!!!!!!!!!!!!!!!!!
4detail:L1|DDDDDDDDDDDDD
5detail:L2|DDDDDDDDDDDDD
6detail:L3|DDDDDDDDDDDDD
7detail:EOF
SSSSS:L1|DDDDDDDDDDDDD
SSSSS:L2|DDDDDDDDDDDDD
SSSSS:L3|DDDDDDDDDDDDD
SSSSS:EOF
hshshshshs:::::::
[[H2|!!!!!!!!!!!!!!!!!, L1|DDDDDDDDDDDDD, L2|DDDDDDDDDDDDD, L3|DDDDDDDDDDDDD, EOF]]
listOrder –is list of line String
List<Order> listOrder = new ArrayList<Order>();
Set<List<String>> hs = new HashSet<List<String>>();
if(poString !=null && poString.size() > 0)
{
headerstart:
for(int i=0;i<poString.size();i++)
{
String s = poString.get(i);
if(s.startsWith("H"))
{
List<String> tempS = new ArrayList<String>();
tempS.add(s);
System.out.println("head:"+s);
for(int j=i+1;i<poString.size();j++)
{
String t = poString.get(j);
System.out.println("detail:"+t);
if(t.startsWith("H"))
{
i = j-1;
hs.add(tempS);
continue headerstart;
}
else
{
tempS.add(t);
}
}
hs.add(tempS);
}
}
In your outer loop: –
When the value of
ireachespoString.size() - 1, then in the inner loop: –The value of
jis initialized withpoString.size(). Now since your condition isi < poString.size()which is still true fori = poString.size() - 1, you move inside the loop, and access the indexjat: –which is
IndexOutOfBounds. In fact, you will get this problem on the first iteration of the outer loop only. Since your inner loop is never breaking, due to that logical error in condition.So, basically, you need to change your inner loop to: –
Note that, I have only changed
itojin the condition part.