Tushar Goel

Ranch Hand

Posts: 934

4

posted 2 years ago

You are trying to compare string and int. Your input variable is string variable and you are comparing it with the integer 1. likewise others.

So both are not equals. Check what happened if you changed integer to string? Does it goes to the loop or not?

Also what happened if it entered in the while loop? No condition to come out of loop so it will run infinitely.

So both are not equals. Check what happened if you changed integer to string? Does it goes to the loop or not?

Also what happened if it entered in the while loop? No condition to come out of loop so it will run infinitely.

posted 2 years ago

- 1

let's use an example...

What happens if the user inputs "1"? you have four conditions:

!input.equals('x') the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(1) the input is 1, so "input.equals" is true, so the negation returns false

!input.equals(2) the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(3) the input is 1, so "input.equals" is false, so the negation returns true

so you basically have:

while (true || false || true || true)

What if we input '7'?

!input.equals('x') the input is 7, so "input.equals" is false, so the negation returns true

!input.equals(1) the input is 7, so "input.equals" is false so the negation returns true

!input.equals(2) the input is 7, so "input.equals" is false, so the negation returns true

!input.equals(3) the input is 7, so "input.equals" is false, so the negation returns true

so now we have

while (true || true || true || true)

etc...

Can you see now how your logic might be wrong?

What happens if the user inputs "1"? you have four conditions:

!input.equals('x') the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(1) the input is 1, so "input.equals" is true, so the negation returns false

!input.equals(2) the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(3) the input is 1, so "input.equals" is false, so the negation returns true

so you basically have:

while (true || false || true || true)

What if we input '7'?

!input.equals('x') the input is 7, so "input.equals" is false, so the negation returns true

!input.equals(1) the input is 7, so "input.equals" is false so the negation returns true

!input.equals(2) the input is 7, so "input.equals" is false, so the negation returns true

!input.equals(3) the input is 7, so "input.equals" is false, so the negation returns true

so now we have

while (true || true || true || true)

etc...

Can you see now how your logic might be wrong?

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

Tushar Goel

Ranch Hand

Posts: 934

4

posted 2 years ago

Fred, if i am not understanding wrong, it took input in a string variable and comparing it will int which returns false not true.

It returns false not true as you mentioned. So in this case:

It returns false not true as you mentioned. So in this case:

*"!input.equals(1) the input is 1, so "input.equals" is true, so the negation returns false"*it should return true.
posted 2 years ago
There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors

It appears you are correct...i was assuming auto-boxing would kick in, but this:

does print false. So above, I should have had this:

!input.equals('x') the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(1) the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(2) the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(3) the input is 1, so "input.equals" is false, so the negation returns true

I think I have it right now...

does print false. So above, I should have had this:

!input.equals('x') the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(1) the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(2) the input is 1, so "input.equals" is false, so the negation returns true

!input.equals(3) the input is 1, so "input.equals" is false, so the negation returns true

I think I have it right now...

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |