String#substring() is meant to throw an IndexOutOfBoundsException
if the first parameter is greater than the second parameter.
Effectively, a substring intends to take the lowest start index, and work its way up to but not including the highest index value you provide it. It has a range of [a, b).
If a > b, then you would be taking a very strange range - and Java has elected to error out on that case.
The culprit(s) are [likely all of] your substring
statements:
statdata.substring(statdata.indexOf("Health") + 7, 3);
Unless the index of "Health"
comes out negative, you'll always be overstepping the bounds of this substring.
Perhaps you want to reverse the order instead:
statdata.substring(3, statdata.indexOf("Health") + 7);
...although, that may be a bandage solution to a larger problem - you're attempting to parse one giant string with substrings, and that's not going to be maintainable in the long run.
Two solutions - one naive, one slightly better than naive - either use a String[]
or a Map<String, String>
to hold your data.
Here's a solution using a Map:
Map<String, String> statData = new HashMap<>();
statData.put("Health", "433 (+78)");
statData.put("Attack damage", "52.3 (+3.9)");
// and so forth