• Post Reply Bookmark Topic Watch Topic
  • New Topic

Logic is Bad or am I missing something (Just an easy while condition)  RSS feed

 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to check for 4 correct inputs.
However this wont do it:
(its an endless loop and I don't know why)



Usually it should go to "Wrong Input" if I enter something as 4 or K or something like that. But everything is wrong.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get it. Haha now when I see it it's embarrassing.
It will always be atleast somehow true.

Thanks for your help I figured a way.
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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: "!input.equals(1) the input is 1, so "input.equals" is true, so the negation returns false" it should return true.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hang on...
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!