不会发生与字符串值的对象值比较

时间:2015-04-03 16:59:13

标签: java

public class ClientServer {

public static void main (String[] args){

    Object[] selectioValues = {"Server "," Client"};
    String initialSection = "Server";
    Object selection = JOptionPane.showInputDialog(null,"login As:","Client server", JOptionPane.QUESTION_MESSAGE,null , selectioValues , initialSection );

    **if(selection.equals("Server"))**{
        server srv = new server();
        srv.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        srv.startRunning();

    }

   **else if (selection.equals("Client"))**{
        String IPServer = JOptionPane.showInputDialog("enter IP:");
        Client capsa;
        capsa = new Client(IPServer);
        capsa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        capsa.startRunning();
   }
}

}

粗体代码不起作用。当我添加断点来检查编译器是否进入IF内部时,它不是。请解释我为什么IF条件没有运行。

1 个答案:

答案 0 :(得分:2)

您的可能选择值为"Server "" Client",但您要与"Server""Client"进行比较。由于前导/尾随空格,字符串不相等。