Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

ArrayList is Picking Up Last Item During Iteration.

 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
  • 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
  • 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: 222
  • Mark post as helpful
  • send pies
  • 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
  • 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: 222
  • Mark post as helpful
  • send pies
  • 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.
 
Pay attention! Tiny ad!
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic