Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
public static void main(String[] args)
{
    boolean runAgain = true;
    while(runAgain)
    { 
        runMyCode();
  
        JOptionPane.showInputDialog( null, " Would you like to exit the application? Enter (y) to exit or any other key to continue ");
    }

    String Answer = " ";
    char answer = Answer.charAt(0);
    if (answer == 'y')
    {
      runAgain = true;
    }
    else
    {
        runAgain = false;
    }   
}


What I have tried:

I have tried swapping the true and false around but no matter what the user enters it still loops even when (y) is selected
Posted
Updated 1-Jun-21 9:43am
v2
Comments
Dave Kreskowiak 1-Jun-21 15:40pm    
You can start by fixing your indentation. It'll make the code far easier to read and debug.

If you actually take care to correct the indentation of your code the problem will likely show itself:
Java
public static void main(String[] args) 
{
    boolean runAgain = true;
    while(runAgain){ // start of the while loop
        runMyCode();
        
        JOptionPane.showInputDialog( null, " Would you like to exit the application? Enter (y) to exit or any other key to continue ");
        
    } // end of the while loop

// so the code from here never gets executed
    String Answer = " ";
    char answer = Answer.charAt(0);
    if (answer == 'y'){
        runAgain = true;
    
    }
    else {
        runAgain = false;
    
    
    
    }   
}
 
Share this answer
 
Comments
Ashwari Pillay 1-Jun-21 15:44pm    
it does execute but when i enter (y) it is supposed to exit the application but it doesnt how do i get it to exit
Richard MacCutchan 1-Jun-21 15:49pm    
Look at the comments I have put in your code. You accept the answer from the user, but then just continue the while loop.
Now that the code is readable, where do you set the value of runAgain inside your while loop?
 
Share this answer
 
Comments
Ashwari Pillay 1-Jun-21 15:49pm    
Im still a beginner. So i made runMyCode() a method that contains the information that will be looped again
Dave Kreskowiak 1-Jun-21 15:52pm    
I'll ask this again. Where in this code do you set the value of runAgain?
while(runAgain)
    { 
        runMyCode();
  
        JOptionPane.showInputDialog( null, " Would you like to exit the application? Enter (y) to exit or any other key to continue ");
    }
Ashwari Pillay 2-Jun-21 9:01am    
i didn't, am i supposed to set a value for runAgain?
Dave Kreskowiak 2-Jun-21 9:46am    
You might want to go back to your class notes and lookup how a while loop works.

So long as the condition in the while clause is true, the code in the braces will run, over and over again, until the clause is no longer true.

In your code, you never set the value of runAgain to false, so that code will run over and over again, forever.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900