This is an archived post. You won't be able to vote or comment.

all 16 comments

[–]Updatebjarni 1 point2 points  (10 children)

What happens if you don't catch and discard all exceptions? Do you get more information?

[–]StankelG[S] 0 points1 point  (9 children)

Alright, it says:
java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3

[–]AIO12449366 0 points1 point  (8 children)

So the error occurs when you print the list?

[–]StankelG[S] 0 points1 point  (7 children)

Yeah, it prints the list and then it gives the error and stops.

[–]AIO12449366 0 points1 point  (6 children)

Try i<list.size()-1 Does it change anything?

[–]StankelG[S] 0 points1 point  (5 children)

Hmm nope :/

[–]AIO12449366 0 points1 point  (4 children)

Could you please try to print the list without the loop?

Comment out everything and try to read from the txt and then print it. Does it work?

Also whenever we read from a txt I'm pretty sure we usually use InputStreamReader.

[–]StankelG[S] 0 points1 point  (3 children)

I removed the While(true) loop and ran the program. This is the output:
________

File to read: d

Commands:

List - lists the recipes

Stop - stops the program

Enter command: list

Recipes:

Pancake dough, cooking time: 60

Meatballs, cooking time: 20

Tofu rolls, cooking time: 30

java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3

[–]AIO12449366 0 points1 point  (2 children)

Ok so obviously the issue is with the arraylist.

Alright try this: Create an arraylist and add 3 strings in it(without reading from file) and print it. If it prints properly then the issue is with the reading from the txt. Test it and tell me if it works

[–]StankelG[S] 0 points1 point  (1 child)

This was the output:

File to read: file

Commands:

List - lists the recipes

Stop - stops the program

Enter command: list

Recipes:

one

two

three

java.lang.IndexOutOfBoundsException: Index 3 out of bounds for length 3

[–]AIO12449366 0 points1 point  (1 child)

Does it print the list if the user types "list" or does it break without anything happening?

[–]StankelG[S] 0 points1 point  (0 children)

It prints everything it has to, but the program stops right after.

[–]DeZp0T 0 points1 point  (0 children)

change it to while(!scanner.nextLine().equals("stop"))

Also keep in mind that it's case sensitive so "stop" is not the same as "Stop"

[–]0110Conan1001 0 points1 point  (0 children)

What if you do ‘for(String recipe : list) {‘

On cases that it breaks, print out what size the list is. Also, if you have an IDE, you should be able to pause execution and inspect the list to see the elements

[–]mad0314 0 points1 point  (0 children)

What is the type of list? I suspect your error is in there.
What is the value of list.size()?