I am trying to read a line from a text file in java. I get a java.lang.ArrayIndexOutOfBoundsException: 1
exception.
Here is my code:
try {
Scanner kb = new Scanner(new FileReader(fileName));
String line = "";
int count = 0;
while (kb.hasNext()) {
line = kb.next();
String[] temp = line.split("#");
System.out.println(temp[1]);
Wedding tempWed = new Wedding(temp[0], temp[1], temp[2], temp[3], Integer.parseInt(temp[4]));
test[count] = tempWed;
count++;
}
} catch (FileNotFoundException ex) {
}
This is the line in the textfile:
Chiquita Sanford#Magee Sosa#2016-11-05#Garden#84
I need to split by the "#", and this partly works.
Java throws the exception when I try to access the element at position 1.
I think this is because there is a space between the first name and the surname, because when I System.out.println(temp[0])
it displays "Chiquita" and not "Chiquita Sanford".
Does Java have some restriction on splitting when there are multiple words in the first array index.