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 Index Out Of Bounds Exception: index 3 , Size 1
is there any possible way to overcome the situation.
myTEXT.txt
PETTY MIND,2023,IS,COMMING
my Code
List<String> TexttoList = new ArrayList<String>();
FileReader myTEXT = 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 3 , Size 1
}
}
it only prints First index(0) and return the entire text
PETTY MIND
2023
IS
COMMING
myTEXTinnew Scanner(myTEXT);? Note thatFileReaderwhich is set to read frommyTEXT.txtfile is namedFileReader scores. Please use edit option and clarify/correct your question so we wouldn't waste your (and our) time on unrelated problems.IndexOutOfBounds Exception: index 3 , Size 1please carefully read your for loop. Note that in its first iteration you are adding one element toTexttoList, but immediately after that, you are trying to read value from position 3, which doesn't exist yet. Maybe you wanted to read it after your loop finished reading all data.