954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Comparing strings held within an object

Hi everyone,

I have a single string - let's call it string A.
I have an ArrayList of objects.
Each object contains three strings.

I want to iterate through the ArrayList of objects and check if any of the strings contained within the objects are the same as string A. Furthermore, I only want to compare the third string in each object with string A.

Basically, I am having problems with the syntax for a string contained in an object, and any help would be great.

Here's the part of code I'm having trouble with:

while( iterator.hasNext() )
{
    if( iterator.string3.equals( stringA ) )
    {
                
    }
}


If anyone could help me with this approach, or give me any ideas for a different approach, that would be a big help.

Thanks!

jiraiya
Light Poster
29 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

Hi everyone,

I have a single string - let's call it string A. I have an ArrayList of objects. Each object contains three strings.

I want to iterate through the ArrayList of objects and check if any of the strings contained within the objects are the same as string A. Furthermore, I only want to compare the third string in each object with string A.

Basically, I am having problems with the syntax for a string contained in an object, and any help would be great.

Here's the part of code I'm having trouble with:

while( iterator.hasNext() )
{
    if( iterator.string3.equals( stringA ) )
    {
                
    }
}

If anyone could help me with this approach, or give me any ideas for a different approach, that would be a big help.

Thanks!


Should I just break this down into 2 steps?

I.e. have an outer loop going through the objects and an inner loop going through the strings?

jiraiya
Light Poster
29 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

that might be the best, you'll have to do that anyway, since you say there are three String objects in every element in the ArrayList

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

It looks like you're having trouble with your iterator. To access the next object you need

while( iterator.hasNext() )
{
    YourObjectType nextObj = (YourObjectType)iterator.next();
    if( nextObj.string3.equals( stringA ) )
    {
                
    }
}


Where "YourObjectType" is of course whatever object you're using that contained the strings.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

Thanks for your help guys!

jiraiya
Light Poster
29 posts since Mar 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You