For loop doesn't iterate as many times as it should (Java)

I’m doing the Spring 2020 challenge, and most of the action of my code happens in a for loop. Even when I make it super simple, “for (int i = 0; i < 5; i++)”, it will only run twice. Also nothing after the for loop runs. I’m using stacks of objects to store the position of each pacman and the closest pellet to each. Here’s some of my code:

for (int c = 0; c < 5; c++) {

            System.err.print(c + " "); //check how many times it runs

            close = 1000.0;

            close1 = 1000.0; //two parameters to determine distance to a pellet

            for (int i = 0; i < visiblePelletCount; i++) {

                int x = in.nextInt();

                int y = in.nextInt();

                int value = in.nextInt(); // amount of points this pellet is worth

                dis = distance (pacs, x, y);

                if (value == 10) {

                    if (dis < close)  {

                        close = dis;

                        pacs.peek().closest(x, y); //sets coordinates of closest pellet


                    check = true;

                } else if (check == false) {

                    if (dis < close)  {

                        close1 = dis;

                        pacs.peek().closest(x, y);





//when I run this i get “0 1”

I guess your code blocks here at the second iteration of the “c loop”, because there are no more inputs to expect.
Why are you trying to loop on inputs ? To read them, just stick to the default code which is provided.

1 Like