Reputation: 10953
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);
}
}
Upvotes: 1
Views: 161
Reputation: 213401
In your outer loop: -
for(int i=0;i<poString.size();i++)
When the value of i
reaches poString.size() - 1
, then in the inner loop: -
for(int j=i+1;i<poString.size();j++)
The value of j
is initialized with poString.size()
. Now since your condition is i < poString.size()
which is still true for i = poString.size() - 1
, you move inside the loop, and access the indexj
at: -
String t = poString.get(j);
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: -
for(int j=i+1; j < poString.size();j++)
Note that, I have only changed i
to j
in the condition part.
Upvotes: 1
Reputation: 10055
Here
for(int j=i+1;i<poString.size();j++)
While doing this, you're storing in j
a value of i
that was incremented by hand, outside of the context of the for that encloses your logic. This could be seen as an "unchecked" incrementation of i
since you use j
to define an index of the same collection you iterate over with the i
index.
Anytime you're doing this, on the last element you'll be using an index that's out of the bound of the array for j
, because if the last index is 7
(remember, lists go from index 0
to index length - 1
like arrays do), j
will be 8 and poString.get(j)
will throw IndexOutOfBoundsException
.
You should always do a range check (if j
is between 0
and poString.size() - 1
, both included on the check) whenever you're manually incrementing the value of an index, or constructing one given different variables at a certain time. The loop will iterate until the condition is no longer met (that is, when i< poString.size()
returns false), but your condition is related to i
, not to j
, allowing for this misfortunes when handling indexes in iterations validating against other indexes. Since the check is not done on the for
loop, you have to do it manually.
In your case, something like this could be the solution:
int j = 0;
for(j = i + 1;j < poString.size();j++)
Since this iteration controls j
directly.
Upvotes: 1