0

I am a beginner Java student. This is the general idea of what I'm doing.

I have a list of things that a user can pick from by inputting the corresponding number. After they input ONE integer, the string next to the item prints as YES. If they decide they don't want it anymore, they have to input that same number again and then string is supposed to change to NO. My nested loop technique is allowing for this change, but changes it right back after reading the next if statement. I have been working on this for a very long time. Can anyone please nudge me in the right direction to identify this problem?

     do
    {
        int num=input.nextInt();  

        if (num == 7)
        {               
            if(s.equals("NO"))  //corresponding string
            {
               s = "YES";
            }
            if(s.equals("YES"))  //same corresponding string
            {
               s = "NO";
            }
        }

    //similar if statements for different conditions 
    //similar if statements for different conditions 


    }while(myBoolean()==true);
1
  • Use an else if block. if (something) {...} else if (something else) {...} Your code is missing the else I think. Commented Jul 4, 2014 at 17:56

3 Answers 3

1

You seem to be missing an else statement.

if(s.equals("NO"))  //corresponding string
{
   s = "YES";
} else if(s.equals("YES"))  //same corresponding string
{
   s = "NO";
}

or if you want to shorten things a bit:

s = s.equalsIgnoreCase("NO") ? "YES" : "NO";
Sign up to request clarification or add additional context in comments.

Comments

0
 do
{

Just add the word "else" in between your "if" statements. In your example, when s is "NO", you change it to "YES". Therefore s is "YES" when you hit your second "if" statement.

Better yet, instead of testing for both values "YES and "NO", just test for one of them and assume the opposite case if the test fails.

E.g.: do { int num=input.nextInt();

    if (num == 7)
    {               
        if(s.equals("NO"))  //corresponding string
        {
           s = "YES";
        }
        else  // <--- This is the only change I made.
        {
           s = "NO";
        }
    }

//similar if statements for different conditions 
//similar if statements for different conditions 


}while(myBoolean()==true);

1 Comment

wow that was so simple! thank you very much! but now for some reason i have to input a number twice before it loops again...do you think something is wrong with my boolean?
0

What you should do is use an else if statement like so

if (num == 7)
{               
    if(s.equals("NO"))
    {
       s = "YES";
    }
    else if(s.equals("YES"))
    {
       s = "NO";
    }
}

If the first if statement is true, it will skip the else if statement. If the if statement is false, it will read the else if statement. You can also have multiple else if statements like so

if(boolean)
{
   ....
}
else if(another boolean)
{
   ....
}
else if(some other boolean)
{
   ....
}

If the if statement and all else if statements are false, you can add an else statement, which will be read

if(boolean)
{
   ....
}
else if(another boolean)
{
   ....
}
else
{
   ....
}

1 Comment

Thank you! The problem now is that it works for changing it from yes to no, but then I can't change it back to yes again without restarting the program

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.