0

I am trying to check if a certain string is in a collection of strings. (The strings in this case are Topics from a activemq broker) So basically I iterate through a topic list and compare those topics with the searched one (save in variable "compare"). "topic.getTopicName()" definately returns a string so I do not get why the varaible count is not set to 1 eventhough the condition is true in one case. So the statement in the IF clause is never executed. Am I overlooking something?

 public ArrayList<String> getTopics() throws RemoteException {



            try {

                 // get Topics as Strings from Broker

                ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
                ActiveMQConnection connection;
                connection = (ActiveMQConnection) connectionFactory.createConnection();
                connection.start();
                DestinationSource ds = connection.getDestinationSource();

                Set<ActiveMQTopic>  topics = ds.getTopics();
                String compare = "Physical";
                int count = 0;


                for(ActiveMQTopic topic : topics){


                        System.out.println(topic.getTopicName());
                        if(compare == topic.getTopicName()) {

                            System.out.println("Found " + topic.getTopicName());
                            count = count + 1;


                        }

                } 
                    if(count == 0){


                        System.out.println("No topic found");

                    }

                    else    System.out.println("Topic found");







            } catch (JMSException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }







            return null;

 }
Julian Herbold
  • 537
  • 9
  • 20

2 Answers2

2

I can't fully debug your code because I'm not sure what is in ActiveMQConnectionFactory but I can provide some advice.

Try adding

if(compare == topic.getTopicName()) {
    System.out.println("Found " + topic.getTopicName());
    count = count + 1;                                                 
}
else {
    System.out.println("Not Found " + topic.getTopicName());
}

The following else statement so you can see what happens when they should be equal. Additionally, I think you should be comparing string using .equals() not == since Strings are objects.

You can see string comparison using == fail in the following code:

public static void main(String[] args) {
        String compare = "Physical";
        String someString = new String("Physical");
        String[] words = {"Test", "Cheese", "Physical", someString};

        for (String s: words) {
            if(s == compare) {
                System.out.println(s + " == " + compare);
            }
            else {
                System.out.println(s + " " + "!= " + compare);
            }
        }
        System.out.println("---------------------------------------");
        for (String s: words) {
            if(s.equals(compare)) {
                System.out.println(s + " == " + compare);
            }
            else {
                System.out.println(s + " " + "!= " + compare);
            }
        }
    }

Prints

Test != Physical
Cheese != Physical
Physical == Physical
Physical != Physical
---------------------------------------
Test != Physical
Cheese != Physical
Physical == Physical
Physical == Physical
DoesData
  • 6,594
  • 3
  • 39
  • 62
1

Strings cannot be compared using the == operator. Specific information can be found here : How do I compare strings in Java?

Change this line of code

if(compare == topic.getTopicName()) 

to this

if (compare.equals(topic.getTopicName())
edm2282
  • 156
  • 6