I am parsing an File , sometimes i get a small line or big line .
Is it possible to append Line with extra tokens incase if the line is small ??
This is my program
private static String[] LISTOFFIELDS = null;
String fields = "A|B|C|D|E|F|G|H|I|J|K|L|M|N|O|P|Q|R|S|T|U|V|W|X|Y|Z";
String line = "OIUU||HHH|INBVC INB|PP|NN|OO|PPPPP||";
String[] totaltokens = line.split("\\|", LISTOFFIELDS.length);
private static HashMap<String, Object> ParsedValues(String[] totaltokens) {
HashMap<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < LISTOFFIELDS.length; i++) {
String fieldName = LISTOFFIELDS[i];
map.put(fieldName, totaltokens[i]);
}
}
As you can see if the line is small i will get
Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: 10
Here i can avoid ArrayIndexOutOfBoundsException if i reduce the String fields to String fields = “A|B|C|D|E|F|G|H|”;
Is it possible to avoid the Exception , without doing any modifications to the String fields ??
Check the length of the string before going to split and getting its substring.