I have something like:
String idCodice = cessionarioCommittente.substring(
cessionarioCommittente.indexOf("<IdCodice>") + 10,
cessionarioCommittente.indexOf("</IdCodice>"));
used to extract the value inside the tag of an XML document represented by the content of the cessionarioCommittente
String variable.
The problem is that the <IdCodice>
tag might not exist in the cessionarioCommittente
String.
So in this case I get this exception:
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -10
at java.lang.String.substring(String.java:1937)
at it.sistinf.ediwea.fepa.LoadFatturePa.run(LoadFatturePa.java:103)
at it.sistinf.ediwea.fepa.TestLoadFatturePa.main(TestLoadFatturePa.java:18)
How can I fix this issue? For example, checking if this value exists in the string?
You can do a preliminary check to make sure the tags you are looking for are present in the String :
By storing the indices in variables you avoid searching for them twice.