Java - 未调用 foreach 中的 If 语句
Java - If statement in foreach not called
我正在尝试检查某个字符串是否在字符串集合中。 (本例中的字符串是来自 activemq 代理的主题)所以基本上我遍历主题列表并将这些主题与搜索到的主题进行比较(保存在变量 "compare" 中)。 "topic.getTopicName()" 肯定是 returns 一个字符串,所以我不明白为什么变量计数没有设置为 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;
}
无法使用 == 运算符比较字符串。具体信息可以在这里找到:How do I compare strings in Java?
更改这行代码
if(compare == topic.getTopicName())
至此
if (compare.equals(topic.getTopicName())
我无法完全调试您的代码,因为我不确定 ActiveMQConnectionFactory
中的内容,但我可以提供一些建议。
尝试添加
if(compare == topic.getTopicName()) {
System.out.println("Found " + topic.getTopicName());
count = count + 1;
}
else {
System.out.println("Not Found " + topic.getTopicName());
}
下面的 else 语句让您可以看到当它们应该相等时会发生什么。此外,我认为您应该使用 .equals()
而不是 ==
来比较字符串,因为字符串是对象。
在下面的代码中可以看到使用==的字符串比较失败:
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);
}
}
}
打印
Test != Physical
Cheese != Physical
Physical == Physical
Physical != Physical
---------------------------------------
Test != Physical
Cheese != Physical
Physical == Physical
Physical == Physical
我正在尝试检查某个字符串是否在字符串集合中。 (本例中的字符串是来自 activemq 代理的主题)所以基本上我遍历主题列表并将这些主题与搜索到的主题进行比较(保存在变量 "compare" 中)。 "topic.getTopicName()" 肯定是 returns 一个字符串,所以我不明白为什么变量计数没有设置为 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;
}
无法使用 == 运算符比较字符串。具体信息可以在这里找到:How do I compare strings in Java?
更改这行代码
if(compare == topic.getTopicName())
至此
if (compare.equals(topic.getTopicName())
我无法完全调试您的代码,因为我不确定 ActiveMQConnectionFactory
中的内容,但我可以提供一些建议。
尝试添加
if(compare == topic.getTopicName()) {
System.out.println("Found " + topic.getTopicName());
count = count + 1;
}
else {
System.out.println("Not Found " + topic.getTopicName());
}
下面的 else 语句让您可以看到当它们应该相等时会发生什么。此外,我认为您应该使用 .equals()
而不是 ==
来比较字符串,因为字符串是对象。
在下面的代码中可以看到使用==的字符串比较失败:
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);
}
}
}
打印
Test != Physical
Cheese != Physical
Physical == Physical
Physical != Physical
---------------------------------------
Test != Physical
Cheese != Physical
Physical == Physical
Physical == Physical