3

This is my first question to this site so I apologize and would appreciate feedback if I post something incorrectly! This is a homework question although I don't seem to be able to tag it that way.

Anyways, the code appears to compile fine (using BlueJ) but gets stuck when it enters the first while loop when I run it. I added some output lines to see where the problem occurs and the first System.out when it enters the first while loop never happens... The JVM just continues working until I force it to reset. I believe my initial while loop should run 4 times then exit with the values I have used (5 students, i starts at 2), but it doesn't appear to do anything at all and I'm at a loss as to what I've done wrong.

Summary of what the program is intended to do when completed.

A series of students walk by a row of lockers.

  • Student 1 opens all the lockers
  • Student 2 closes every second locker
  • Student 3 reverses the state of every third locker, etc. for the number of students

I am aware I haven't set the boolean locker flag to flip properly yet and intend to use a variation of !myBool to do so within the second while loop - but first I want to ensure my while loops work at all. Hoping I'm missing something simple due to staring at it for too long!

import java.util.Arrays;

public class Lockers
{
    public static void main (String[]args)
    {
        // Size of lockerArray
        int arraySize = 5;
        // Set up new lockerArray
        boolean[] lockerArray = new boolean[arraySize];

        // Variable for number of students in the exercise
        int studentNumber = 5;

        System.out.println("Student 1 opens all the lockers.\n");// Outputs, good
        // Student 1 opens all the lockers
        // Boolean values for lockerArray true = OPEN; false = CLOSED
        Arrays.fill(lockerArray, true);
        System.out.println(Arrays.toString(lockerArray)); // Outputs 5 true, good

        // Set the student counter at 2 (Student 1 has already made their pass)
        int i = 2;
        // Loop until you run out of students
        while (i <= studentNumber);
        {
            System.out.println("Student Number " + i + " is making their pass.\n");// NEVER HAPPENS - have to reset JVM to stop program
            // Set up a variable to control the sequence required (i.e., Student 2 every second locker,
            // Student 3 every third locker, etc.
            int j = i;
            while (j <= arraySize);
            {
                System.out.println("Student is changing the status of locker number " + j + ".\n");
                // Reverse the flag at each locker for which the statement is true for this student number
                // Need to reduce the variable by 1 as locker 1 would be sitting in lockerArray[0] position
                lockerArray[j-1] = false;
                // Increment locker number by the value of the student in the sequence
                j = j + i;
            }
            // Increment the student count
            i++;
        }
        // Print the final array status
        System.out.println(Arrays.toString(lockerArray));
    }

}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Amber
  • 41
  • 3
  • 1
    The [tag:homework] tag is disabled because many people inferred that homework questions are not allowed. Homework questions are perfectly fine as long as the asker shows their attempts and demonstrates effort solving the problem themselves. You've done that, so no worries here! – John Kugelman Jan 25 '14 at 20:01
  • Oh, wonderful thank you!!! Removing the semi-colons fixed the issue. Normally my problem is forgetting them :) This site is great, the response speed was incredible. – Amber Jan 25 '14 at 20:02

2 Answers2

8

Your while loops have semi-colons after them.

while (i <= studentNumber);

This is causing the infinite loop, since your i variable can't change.

Makoto
  • 104,088
  • 27
  • 192
  • 230
-3

You need brackets around your while loop unless it will run infinitely Try This:

while (i <= studentNumber){
// action
}
  • 1
    The loop already has brackets. The extraneous semicolon is the problem. – John Kugelman Jan 25 '14 at 20:04
  • @no_answer_not_upvoted: What're you talking about? This wasn't the first answer... – Makoto Jan 25 '14 at 20:07
  • 2
    And if it were, that'd be a terrible reason to upvote. – John Kugelman Jan 25 '14 at 20:12
  • @Makoto it was TylerRutherford120 (the answerer's) first answer on stackoverflow :) – necromancer Jan 25 '14 at 21:32
  • @JohnKugelman if that had been my reason, yes, i agree :) i meant the answerer's first answer on stackoverflow. and it is not entirely incorrect in that adopting it or even comparing it to the asker's code will immediately lead to a solution. (note the lack of space between the close parenthesis and open curly brace -- not good style but effective at zeroing in on the extraneous semicolon) – necromancer Jan 25 '14 at 21:34