So i want to read a String and an int from a text file and it gets me java.lang.ArrayIndexOutOfBoundsException: 1
public class GetNameAndNumber{
private ArrayList <NameAndNumber> list = new ArrayList <NameAndNumber>();
BufferedReader buf = new BufferedReader(new FileReader("NameAndNumber.txt"));
String linie = buf.readLine();
while(true)
{
linie = buf.readLine();
if(linie == null)
break;
else
{
String split[] = linie.split(" ");
NameAndNumber nan = new NameAndNumber(split[0], Integer.parseInt(split[1]));
list.add(nan);
}
}
}
the "NameAndNumber" class has a String and an int
and this is the text file:
John 1
David 0
Ringo 55
What i don't know is why this one gives me an error, but when i read 2 strings and then an int like
NameAndNumber nan = new NameAndNumber(split[0], split[1], Integer.parseInt(split[2])); - this "NameAndNumber" having two strings and an int
for a text file like
Johnny John 8
Mathew John 0
it gives me no errors and stores the values correctly. why ?