What’s wrong with this while? The || is not working

Asked

Viewed 117 times

3

Scanner input = new Scanner(System.in);
String str = input.nextInt();
while ( (str.nextInt() != 5) || (str.nextInt() != 10) ){
    str = input.nextInt()
}

The code should invoke the method nextInt() every time the user typed some number other than 5 or 10. What’s happening here is that when I type 5 or 10 it keeps repeating, invoking the method nextInt().

  • 2

    Welcome to Sopt. What do you want with this comparison? Edit the question and explain better what this code does or should do.

  • 2

    There is nothing wrong with the fact that we do not know what is expected ;). A brief description of what is wrong or from the input which is the result obtained and which is the expected amount already helps a lot!

  • The code should invoke the nextInt() method every time the user enters a number other than 5 or 10. What’s happening here is that when I type 5 or 10 it keeps repeating, invoking the nextInt method().

  • 1

    So it’s working perfectly. If it’s to continue if it’s different from 5, OR if it’s different from 10, it’s right.

  • 1

    Tip: What you want is to be executed when the number is "other than 5 and 10" and not "different from 5 or of 10"

  • 6

    For you to understand better: If it’s 5, it’s different from 10, then go on. If it’s 10, it’s different from 5. So go on. That’s what you determined with this ||.

  • 2

    Try it like this while ( (str.nextInt() != 5) && (str.nextInt() != 10) )

  • 2

    Ow!!! Thank you, you solved my problem, but I’m still confused about the "e" and "ou". My logic is horrible. I’m sorry about the misspelled question

  • 1

    What confuses a little is the fact of using NOT !, because the logic reverses. Another way of writing is this: while ( !( str.nextInt() == 5 || str.nextInt() == 10 ) ) - "Until it is 5 OR 10, run" - We are using an OR within parentheses, and denying the result of OR as a whole.

  • 1

    Do this exercise: In the sentence "3 is different from 5 ? different from 10" what article(E/OU) you need to use to make sense of it?

  • 1

    Hey, Massa, thanks for the help you guys gave me. In this, I ended up remembering the truth table in the subject of logic in mathematics, which helped me understand better, too. V and V = VV and F = FF and V = FF and F = F-----------------V or V = VV or F = V --> so the loop was never false. F or V = VF or F = Ftem others too, which I have forgotten now, like the "what if", "implies" and others, but I will take a look later. Thanks, guys, you’re great.

Show 6 more comments

2 answers

-1

Scanner input = new Scanner(System.in);
String str = input.nextInt();
while ( str != 5) || str != 10) ){
    str = input.nextInt()
}
  • It would be interesting to add some explanation to your answer, even if this code is the solution to the question.

-1

What you need to understand is that you have three commands nextInt() in that passage.

So the program can call twice nextInt() before entering the ìf.

I understand from your explanation that you want to call nextInt() once, see if the return is 5 or 10, and only then call nextInt() a second time. Something like this:

bool continua = false;
do
{
    int primeiraLeitura = input.nextInt();
    if ( primeiraLeitura != 5 || primeiraLeitura != 10 )
    {
        int segundaLeitura int.NextInt();
        continua = true;
    }
} while (continua);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.