简体   繁体   中英

the object value comparison to a string value does not happen

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

}

The code in bold doesn't work. When I added breakpoints to check whether the compiler is going inside IF then it was not. Please explain me why IF condition is not running.

Your possible selection values are "Server " and " Client" but you compare to "Server" and "Client" . The strings are not equal because of the leading/trailing spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM