Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

ArrayList is Picking Up Last Item During Iteration.

 
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I created a simple web service which consists of a Person class containing a List of Car classes.

Person class:



Car class:



Created a different object to store my inquries.



Here's my actual client (a Jersey based Restful Web Service):



When I call the getPerson() method as follows:



It returns a 201 containing this JSON Object (which is what I want):



However, when I call the getCar method() as follows:



I get the following JSON object (with the second item in the ArrayList being added!):



I wanted it to be Toyota instead of Ford.

If you can tell, I printed out the value of the car.getMake() String and inside the console / shell, it ways says this:

car.getMake(): Toyota

car.getMake(): Ford

So, it seems that its iterating through the list but adding the last item (or just next item)??

What am I possibly doing wrong? Thank you for taking the time to read this...
 
Ranch Hand
Posts: 34
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Dekker wrote:




1. If loop is not coded correctly. semicolon is added at the end of if condition.
by doing so, if condition is evaluated and does nothing. and the next two lines are executed for all cars irrespective of if result.
2. you may have to call "name.equalsIgnoreCase(car.getMake)"

If you can tell, I printed out the value of the car.getMake() String and inside the console / shell, it ways says this:

car.getMake(): Toyota

car.getMake(): Corolla // I assume you meant Ford here

 
James Dekker
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for responding, Suresh!

Yes, I meant "Ford" instead of "Corolla" and even edited my original post to reflect that.



1. If loop is not coded correctly. semicolon is added at the end of if condition.
by doing so, if condition is evaluated and does nothing. and the next two lines are executed for all cars irrespective of if result.

2. you may have to call "name.equalsIgnoreCase(car.getMake)"



1. Can you elaborate on this? I think Eclipse might have added this (the ";") in... This does look odd... Should the semicolon be removed?
It does seem like the conditional "if" isn't picking up or getting invoked properly because both cars are printed to the console.

2. Will try your suggestion with the "name.ignoreCase(car.getMake)"

With thanks!
 
Ranch Hand
Posts: 147
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

James Dekker wrote:
1. Can you elaborate on this? I think Eclipse might have added this (the ";") in... This does look odd... Should the semicolon be removed?
It does seem like the conditional "if" isn't picking up or getting invoked properly because both cars are printed to the console.



In Java, a semicolon ends a statement. So your program evaluates the if, but there is nothing it does afterwards (because of the ;, which is basically an empty statement on the same line). You should remove it, yes.
 
James Dekker
Ranch Hand
Posts: 229
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jan & Suresh,

Thank you! The semicolon was the thing that was breaking my code.

Good eyes! Can't believe I missed that.
 
Beauty is in the eye of the tiny ad.
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic