• Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
Edin Tin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • 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
  • 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~

 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • 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?

 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • 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 is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!