-2

I have an infinite loop that waits for a method to update a boolean, and while this seems to all work fine at school it doesn't work at home. Not sure if that really means it's computer-based or what, since it also works with the print statement in there.

while(running){
    //System.out.println(running);      
}

...

public static void passwordCheck(String pass){
    if(pass.equals(password)){
        correctPW = true;
    }
    else if(pass.equals(overKey))
        PWCoder.override(password);
    running = false;
}

...

public void actionPerformed(ActionEvent e){
    String temp = new String("");
    for(int x=0;x<passwordInput.getPassword().length;x++)
        temp+=passwordInput.getPassword()[x];
    PWCoder.passwordCheck(temp);
}

As I said, this code works with the print statement uncommented, but that's rather ugly. What I'm going for here is a password screen that you type it in, if you're wrong a different window opens(code not shown), or you can type the override password which calls the override method. I've used many debug statements, running gets set to false in the method, I guess the while loop just doesn't pick it up. Sorry the code is a tad out of order. While loop with problem, method that should get the loop to end, and the method in another class that calls said method.

moeTi
  • 3,884
  • 24
  • 37
Syxx
  • 19
  • 1
  • 5
  • I the way I see it you are not changing the value of `runinng` within the while... so it cannot stop – iberbeu Mar 05 '13 at 16:59
  • if you are checking a non-volatile variable in a tight loop, you may never see any changes performed by another thread. Even if the variable is volatile, it's a bad idea for performance reasons – John Dvorak Mar 05 '13 at 17:00
  • My confusion came because this works just fine on my school's computer, but my home computer has problems running it. I do realize it may be too fast, but I can't think of what I can put in the loop to slow it down but not make a huge mess of something else. – Syxx Mar 05 '13 at 19:26
  • are you working with threads or how are you waiting to change the variable. Anzwaz the code makes no sense. You should use events and not whiles for doing such a thing – iberbeu Mar 05 '13 at 20:47
  • We recently started doing GUI's with swing and awt in school, so I decided to apply it to a project of mine. I have another class that brings up the menu when the object is created, and then the main method goes into the while loop. When the password is inputted to the PasswordField and the submit JButton is pressed, the event code that is shown fires. Whether or not the password was right, the variable running should be set to false and the next action will be applied. – Syxx Mar 05 '13 at 20:59

1 Answers1

0

A number of issues, this should work

 public static void passwordCheck(String pass){
        if(pass.equals(password)){
            correctPW = true;
            running = true;
        }
        else if(pass.equals(overKey)) {
            PWCoder.override(password);
        running = false;
    }
    }

Also, you may consider just using break, to get out of your loop on success.

Chris
  • 923
  • 1
  • 8
  • 11
  • This is the same as he did. If you look it better you will realize that the sentence `running = false;` is out of the `else if` – iberbeu Mar 05 '13 at 16:58
  • @iberbeu, that is exactly what I fixed. – Chris Mar 05 '13 at 16:59
  • in this code `else if(pass.equals(overKey)) PWCoder.override(password); running = false;` the last sentence is out of the `else if` so it will be allways called. It doesn't matter if it goes through the `if` or through the `else` – iberbeu Mar 05 '13 at 17:02
  • @iberbeu I feel I'm repeating myself and you are not looking at what I posted. – Chris Mar 05 '13 at 17:06
  • I need it outside of the loop though. If the password is correct, I still need running to be false so it can go to the next step. Also I did attempt a break, using while(running){ if(running==false) break; } but it may still be too fast for the loop to catch the change – Syxx Mar 05 '13 at 19:23
  • @Chris - Now you changed it. It makes sense now but not before. You were doing the same twice :) – iberbeu Mar 05 '13 at 20:44