I try several times to convert the text in a File into List and print some elements of the List according to reference index but I can't, it throws IndexOutOfBounds Exception: index 3 , Size 1
is there any possible way to overcome the situation.
myTEXT.txt
BUK,2023,IS,COMMING
my Code
List<String> TexttoList = new ArrayList<String>();
FileReader scores = new FileReader("myTEXT.txt");
Scanner scanText = new Scanner(myTEXT);
scanText.useDelimiter(",\\s*");
while(scanText.hasNext()) {
for(int d = 0; d<4; d++){
TexttoList.add(d , scanText.next());
System.out.println(TexttoList.get(3));
//IndexOutOfBounds Exception: index 4 , Size 1
}
}
it only prints First index(0) and return the entire text
BUK
2023
IS
COMMING