Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

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

 
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
 
Bartender
Posts: 1844
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~

 
Marshal
Posts: 5593
384
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?

 
Marshal
Posts: 58378
178
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!