• Post Reply Bookmark Topic Watch Topic
  • New Topic

for : each loop not working?  RSS feed

 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm trying to use a for:each loop for the first time to write a method to find the number of even integers in an array.
My main calls:


and this is my method:



This prints 0.0 as the number of evens, which is not right.
Thanks for any help!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int / int yields an int result, which is truncated or rounded down. So, for example, 9 / 10 results in 0. The fact that you assign the result to a double doesn't matter. The int division expression's result is always an int, and that int is 0 before the promotion to double 0.0.

You could declare at least one of those variables to be double, or, a better approach IMHO simply cast one of them when you divide:

 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While Jeff has given you the answer, I'd like to make the following suggestion...

Your assumption was that your for:each loop wasn't working, but that isn't really true. Why did you think that? I'm assuming it was because your method was not returning the correct value, and you jumped to the loop not properly iterating through the entire array.

Next time you have a problem like this, spend a few minutes putting tons of "System.out.println()" statements in your code. I would have done something like this:


Then I can see exactly what my code is REALLY doing. By doing this, you would have seen your loop IS working correctly, and that your calculation was off.

I purposely leave these debugging print statements all the way to the left, so they can be found easily later and removed/commented out. When you get a little further along in your experience, you'll learn about using various logging APIs that let you dynamically turn the logging on and off, so you don't have to keep putting this in and taking them out.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:
Your assumption was that your for:each loop wasn't working, but that isn't really true. Why did you think that? I'm assuming it was because your method was not returning the correct value, and you jumped to the loop not properly iterating through the entire array.

Next time you have a problem like this, spend a few minutes putting tons of "System.out.println()" statements in your code.


... rather than just assuming where the problem lies.

++

Excellent advice, couldn't agree more.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!