Java - 如果没有调用foreach中的语句

时间:2017-08-18 13:02:17

标签: if-statement foreach jms activemq broker

我正在尝试检查某个字符串是否在字符串集合中。 (在这种情况下,字符串是来自activemq代理的主题)所以基本上我遍历主题列表并将这些主题与搜索到的主题进行比较(保存在变量"比较")。 " topic.getTopicName()"肯定会返回一个字符串,所以我不明白为什么在一种情况下条件为真时,可变计数没有设置为1。所以IF子句中的语句永远不会执行。我忽略了什么吗?

 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;

 }

2 个答案:

答案 0 :(得分:2)

我无法完全调试您的代码,因为我不确定message = if count < 2 'message 1' else 'message 2' end puts message.center(75) 中的内容,但我可以提供一些建议。

尝试添加

ActiveMQConnectionFactory

以下其他声明,以便您可以看到它们应该相等时会发生什么。另外,我认为您应该使用if(compare == topic.getTopicName()) { System.out.println("Found " + topic.getTopicName()); count = count + 1; } else { System.out.println("Not Found " + topic.getTopicName()); } 而不是.equals()来比较字符串,因为字符串是对象。

您可以在以下代码中使用== fail查看字符串比较:

==

打印

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);
            }
        }
    }

答案 1 :(得分:1)

使用==运算符无法比较字符串。具体信息可在此处找到:How do I compare strings in Java?

更改此行代码

if(compare == topic.getTopicName()) 

到这个

if (compare.equals(topic.getTopicName())