• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why the code doesn't get out of the loop  RSS feed

 
Robert Woehl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why as I enter a valid double number it repeats the "try" part again if at the end of the "try" I put (test = false)? Shouldn't it do the calculus and stop executing the loop?
and
Why does it get into an infinite loop when I enter and invalid number? It should print out "Invalid Number" and start the loop again right? Why doesn't it allow me to re-enter the double again?
I programmed in Delphi in the past, so there must be something to do with this exception.
I just want to enter a double value and:
- If the double is a double do the calculus, else print "Invalid Number" and ask the user to enter the number again. While the double is not a valid double print "Invalid Number" and ask to type it again.

 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


You've made one of the standard beginner errors here, I'm afraid. The expression "test = true" is an assignment expression, so it assigns true to the "test" variable and its result is true. Always.

I expect you wanted to compare the variable "test" to see if it was true. The comparison operator is ==, not =, but to avoid errors like that one you should just use the boolean variable as a boolean expression. Like so:



Also the Ranch Office forum is for questions and comments about the Ranch, not for questions about your code, so I'll move this post to one of the regular forums.

 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I programmed in Delphi, too, once upon a time and while test = true would not have been a good way to write that code in Delphi either. The way Paul suggests to write it in Java applies in Delphi as well. So now you've learned something to improve your code in both languages.
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't double-space everything; that makes the post harder to read not easier. Always use the code button; I have edited your post and doesn't it look better
Don't write == true of or == false anywhere. As Paul C says simply use the boolean value.
Everybody else makes that mistake, and you have just been unlucky writing = as well.

Edit: correct spelling error.
 
Robert Woehl
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:I programmed in Delphi, too, once upon a time and while test = true would not have been a good way to write that code in Delphi either. The way Paul suggests to write it in Java applies in Delphi as well. So now you've learned something to improve your code in both languages.


My goodness, that was bad. Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!