我正在开发一个使用swing表来存储数据的eclipse中的定时器应用程序。 我遇到的主要问题是,如果if语句保持返回true,即使这些变量中的值相同(如输出所示)。什么可能导致这个?为什么如果语句返回真?
这是我的代码片断:
String currentPID = lblCurrentPID.getText();
for (int i = 0; i < tableActive.getRowCount(); i++) {
String currentValue = (String) tableActive.getValueAt(i, 2);
System.out.println(i + ": Current Value: " + currentValue + " - Current PID: "+ currentPID);
if (currentPID != currentValue) {
System.out.println("The value and PID are not the same for some reason!");
}
else {
tableActive.setValueAt(ts1.getOverallTotalTime(), i, 3);
System.out.println(i + ": Row Was Changed!");
}
}
这里是输出:
0:当前值:Test3的 - 当前PID:Test3的
值和PID是不一样的因为某些原因!
1:当前值:12345 - 当前PID:Test3的
价值和PID是不是出于某种原因一样!
2:当前值:54321 - 当前PID:Test3的
价值和PID是不是出于某种原因一样!
尝试'!currentPID.equals(currentValue)'。 – OldCurmudgeon 2013-03-13 14:43:17