java - Why Is While Loop Not Working?

I have the following nested class inside a class that implements MouseListener:

public void plusOne()
    {       
    int reference = 0;
    int status = 0;
    System.out.println("BEGIN");
    System.out.println(dateArray[reference].selected);

    while (dateArray[reference].selected = false)
        {
        reference++;
        System.out.println("SUCCESS");
        }

        while (dateArray[reference].selected = true)
        {
        reference++;
        }   

        while (true)
        {
            if (dateArray[reference].status == 2)
            {
            dateArray[reference].status =1;
            status =2;
            break;
            }
            if (dateArray[reference].status == 3)
            {
            dateArray[reference].status =0;
            status = 3;
            break;

        }
        dateArray[reference].selected = true;
        reference++;    
        dateArray[reference].status = status;
        }


    }

System.out.prinln("BEGIN") is executed. System.out.prinln(dateArray[reference].selected) is also executed. It should display the value "false" but it ALSO displays an error message of type

"Exception in thread "AWT-EventQueue-0" false

The next while loop is not executed. Help Please!

3 Answers

  1. Chris- Reply

    2019-11-14

    you wrote wrong syntax in java, trace your code and try like example code below

      while (dateArray[reference].selected == false){
    
      while (dateArray[reference].selected == true){
    
  2. Colin- Reply

    2019-11-14

    Your main problem is that you're trying to force linear command line program type code into a single threaded event-driven programming environment, and this will likely completely freeze your GUI. For example this:

    public void plusOne() {       
        int reference = 0;
        int status = 0;
        System.out.println("BEGIN");
        System.out.println(dateArray[reference].selected);
    
        while (!dateArray[reference].selected) {
            reference++;
            System.out.println("SUCCESS");
        }
        // ... here
    

    Is fine in a simple console program since program code flow is dictated solely by you the programmer, but note that you'd likely have code within the while loop for user to enter code, and the program would wait patiently for the user to do this, then the loop would check the condition and decide whether to re-ask the user for input or proceed beyond the loop.

    But this won't work in a single threaded Swing program since that while loop will block the Swing event dispatch thread and would then completely freeze the GUI. The solution is to get rid of the while loop and instead use if blocks and instance fields and vary your method's response depending on the state of the fields. The details of how you would implement this would depend on the details of your program and code, something we don't really know yet, but that you should tell and show us.

  3. Cosmo- Reply

    2019-11-14

    The = should be ==

    public void plusOne()
    {       
    int reference = 0;
    int status = 0;
    System.out.println("BEGIN");
    System.out.println(dateArray[reference].selected);
    
    while (!dateArray[reference].selected) //-----------
        {
        reference++;
        System.out.println("SUCCESS");
        }
    
        while (dateArray[reference].selected)  //-------------
        {
        reference++;
        }   
    
        while (true)
        {
            if (dateArray[reference].status == 2)
            {
            dateArray[reference].status =1;
            status =2;
            break;
            }
            if (dateArray[reference].status == 3)
            {
            dateArray[reference].status =0;
            status = 3;
            break;
    
        }
        dateArray[reference].selected = true;
        reference++;    
        dateArray[reference].status = status;
        }
    
    
    }
    

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>