• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how come my counter isn't counting and return properly?

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to use an iterator instead of a foreach loop to go through a list of inventories to see if a car is rearDrive and count how many are rearDrive but somehow I seem to be missing something that the counter isn't working as expected.



can someone please give me a hand? Thanks
 
Bartender
Posts: 1845
10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Iterator? What Iterator are you using here?

Maybe you should call: inventory.iterator() ?

If you had a for loop, I would expect to see :


To convert a for loop to a while loop, you have to have all of the elements
- initialization of your loop counter (int i=0)
- a condition check to end the loop (i<inventSize), and
- code to increment each time through the loop (i++).

Which one of those three is missing from your code?
>
 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stefan Evans wrote:Iterator? What Iterator are you using here?

Maybe you should call: inventory.iterator() ?

If you had a for loop, I would expect to see :


To convert a for loop to a while loop, you have to have all of the elements
- initialization of your loop counter (int i=0)
- a condition check to end the loop (i<inventSize), and
- code to increment each time through the loop (i++).

Which one of those three is missing from your code?
>


OMG! thanks I am so stupid to miss the obvious!
I wanted to use for loop but then the assignment says no foreach loop and he only taught us foreach and while loop instead of for loop. That's why trying to do it with while loop instead of going with for loop.
oh gosh~ I am so stupid~

 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Edit Tin wrote:I wanted to use for loop but then the assignment says no foreach loop and he only taught us foreach and while loop instead of for loop. That's why trying to do it with while loop instead of going with for loop.


I'm not sure you understood what Stefan just said.
You are using "while" loop instead "for" - that's fine. But you're missing one important part of it. Do you understand which one?

Also think, how could you simplify this line?

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Never write == true or == false which are both poor style and error‑prone. Every now and again we see somebody write = by mistake.
Not if (b == true)  ... but  if (b) ...
Not if (b == false) ... but if (!b) ...
You should writeinstead.

Can you do it with a stream? Would this work?I can see one mistake which I know about in that code.
 
It's a tiny ad. At least, that's what she said.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic