1

Awnsers below worked, thanks guys!

boolean Again = true;
while (Again = true) 
{
    String input = JOptionPane.showInputDialog(null, "Input Za Number");
    try 
    {
        int input2 = Integer.parseInt(input);
        int R1 = 1;
        int R2 = 1;
        while (R2 < input2)
        {
            R1 = R1 * 2;
            R2 = R2 + 1;
            JOptionPane.showMessageDialog(null, R1);
        }
        String Ag = JOptionPane.showInputDialog(null, "Do it again? (Y/N)");
        if (Ag.equals("N"))
        {
            Again = false;
        }
    }
    catch(Exception e)
    {
        JOptionPane.showMessageDialog(null, "What is  '" + input + "'?!?! Enter an actual number.");  
    }
}

I put the part thats not working as I hope in bold. Its the "if (Ag.equals("N"))". Im trying to make it so if a user inputs "N", then the program will stop running. Otherwise it will loop and keep going. Problem is, even if I type N, it will loop. I am getting no errors. Please help, I am much confuse :(

3
  • 3
    while (Again = true) must be while (Again == true) (or simply while (Again)) Commented Mar 2, 2016 at 15:28
  • Thanks Andreas, your solution fixed it :D Commented Mar 2, 2016 at 15:29
  • In the end, you are victim of bad practices. Simply avoid someBool == true (or false). Just do if (someBool) or while, or ... Besides: read about naming conventions - variable names start with lower case letters. And: give the things you are using meaningful names: "input2" means nothing. R1 means nothing. R2 means nothing. Commented Mar 2, 2016 at 15:41

2 Answers 2

4

This is a prime example as to why professional developers will always use

if ( null == object )

over

if ( object == null )

In the event of a mistype, such as the following

// as posted
while ( Again = true ) // works, always true

// the other way
while ( true = Again ) // compiler error

the way as posted would be reassigning the value true to Again every time the loop is reiterated; if the statement had been written the other way, the compiler would have thrown an error, stating that you can not assign a value to true.

With just the single equals, you are reassigning the value of Again to true every time you get here, where as the double equals checks for equality.

In a case such as this, you really don't even need the true, you could simply be using

while ( Again )
// or again
while ( true == Again )

relevant

Sign up to request clarification or add additional context in comments.

Comments

2

When you do something like

while (Again = true) 

you are actually assigning the variable to true,

that is making your while infinite...


Do instead

 while (Again)  // since is boolean you dont need to compare against a true value..

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.