Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Conditional statements on Array list objects

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Conditional statements on Array list objects

    New to arrayLists and confused.

    I have 2 classes. One is Property the other is Letting Agent.

    Property creates property objects with 7 private variables. Letting Agent creates an arrayList of property objects.

    I have managed to use external method calls to methods in the Property class in the Letting Agent class to update objects in my arrayList.

    Now I am trying to implement a property search method in Letting Agent that takes 3 parameters and checks them against 3 of the fields of each object in my arrayList until one that matches all three is found. I am doing this using a while loop.

    The problem I am having is that the while loop needs to contain an if/else statement that performs comparisons of the 3 parameters taken when the method is called to the values stored within the fields of that object.

    public void propertySearch( char location, double maxMonthlyRent, int minNumberOfBedrooms)
    {
    int index = 0;
    boolean propertyFound = false;
    while(!propertyFound && index < properties.size())
    {
    Property property = properties.get(index);
    if(location == property.location && maxMonthlyRent >= property.monthlyRent)// and same for bedrooms
    {
    property.displayProperty( // this method is in my Property class
    propertyFound = true;
    }
    else
    {
    index++;
    }

    }


    The problem is that I cannot perform my comparison as the fields of the Property class are private and I amsure that they were supposed to stay this way. Is there another way of me performing my if/else statement against the fields of the objects in my arraylist in the Letting Agents class?

    I ONLY need to perform a comparison not actually alter the data stored in the fields in any way so I was thinking that there might be a way of getting the values of the fields that I am overlooking.

    This has me really stuck so would really appreciate it if someone can give me kick in the right direction. Please ask me if you need to know more.

    Thanks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Conditional statements on Array list objects

    Does the Property class have getters for those variables?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Conditional statements on Array list objects

    Yes it has methods like this for each field of the Property class

    public double getRentOwed()
    {
    return rentOwed
    }

    I think I can see where this is going. Do I have to call that method in my LettingAgent class in
    order to perform comparisons with the value stored in rentOwed?

    eg.

    double rentOwed= property.getRentOwed()
    if( rentOwed == null || 0)
    {
    statement
    }

    I'm going to try that for now. I thought that return statement were just for returning the value to the user
    but can they also be used to return the value to another class?

    Thanks

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,400
    My Mood
    Hungover
    Thanks
    144
    Thanked 628 Times in 538 Posts

    Default Re: Conditional statements on Array list objects

    Quote Originally Posted by jaguarpaw View Post
    I'm going to try that for now. I thought that return statement were just for returning the value to the user
    but can they also be used to return the value to another class?
    Absolutely! Note that you can use the value returned from the method directly, without storing it in a variable first:

    if(property.getRentOwed()== 0)

    Also note that the syntax for your if statement is a little off (and primitives can't be null anyway), but you can cross that bridge when you get there.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. instantiating class objects from an array
    By BadAnti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 12th, 2011, 03:27 PM
  2. How to store objects from a class inn an array?
    By dironic88 in forum Object Oriented Programming
    Replies: 1
    Last Post: April 7th, 2011, 02:42 PM
  3. Operate on list`s objects
    By MryJaho in forum Java Theory & Questions
    Replies: 5
    Last Post: February 24th, 2011, 04:44 AM
  4. [SOLVED] Array of objects, invoking constructor for one changes others
    By BigFoot13 in forum Object Oriented Programming
    Replies: 4
    Last Post: October 24th, 2010, 01:30 PM
  5. [SOLVED] Creation of objects of Array in Java
    By sadi_shihab in forum Collections and Generics
    Replies: 4
    Last Post: July 9th, 2009, 01:38 PM