samedi 30 janvier 2016

Java: if statement works only after a system.out.println()

i need your help to solve this strange behavior! i've a class, that contains a public boolean variable: running. Through a button i can set "running" true or false. In the main class, in the main function i have what follows:

the point is that written in this way the code doesn't work! It works if i add a System.out.println(running); before the if condition! Do you know why?

i've also tried creating a method, inside the class, boolean isRunning() that return a boolean but it does not work.

public static void main(String[] args) {

Gui frame = new Gui();
frame.setVisible(true);


    String received = "";

    while(true){

        if (running ==  true){
            System.out.println(frame.running);

            try {
                Thread.sleep(1000);                
                } 
            catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }       
            ricevuti = frame.arduino.receivedstring;
            if(ricevuti.isEmpty()){
               // do something
                }
            else {
                int [] intArray = null;
                intArray = frame.arduino.parsing(ricevuti, " ");
                System.out.println(intArray.toString());
            }

        }
    }
}

Thank you so much!

Aucun commentaire:

Enregistrer un commentaire