简体   繁体   中英

Java can't reach case 283 in switch

I had a weird problem with java, while working with Sockets I created a case LOGOUT: (283) for the server to handle (client sends different ints to specify a service request to server), but in my switch the LOGOUT case was unreachable for some reason (IntelliJ told me that this statement was unreachable). So I tried System.out.println(ServiceId) and it was always a different number (not a random one) from 283, it was really weird. I solved this problem by changing the number. Obviously the number 283 wasn't in no other variable. Could it be Java that use a final static int with value 283 for something else?

PS : Could it be that switch has maximum number of statement where this number is less than 283?

    private static void getService(Socket s) throws IOException {
        InputStream inputStream = s.getInputStream();
        int servId = inputStream.read();
        OutputStream outputStream = s.getOutputStream();
        Runnable task;
        System.out.println("***************** " + servId); // this servID should be the number 283 but it's not.
        switch(servId) {
            case REPLY_EMAIL:
            case FORWARD_EMAIL:
            case REPLY_ALL_EMAIL:
            case WRITE_EMAIL:
                outputStream.write(SUCCESS_RESPONSE);
                task = new EmailReceiver(s,lock, servId, serverLog);
                threadPool.execute(task);
                break;

            case LOGOUT:
                outputStream.write(SUCCESS_RESPONSE);
                threadPool.execute(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            String user = Utils.getUser(s);
                            serverLog.writeLog("L'utente " + user + " si è disconnesso.");
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;

            case READ_ALL_EMAILS:
                outputStream.write(SUCCESS_RESPONSE);
                task = new EmailReader(s, serverLog);
                threadPool.execute(task);
                break;

            case USER_LIST:
                outputStream.write(SUCCESS_RESPONSE);
                task = new UserGetter(s, serverLog);
                threadPool.execute(task);
                break;

            case LOGIN:
                outputStream.write(SUCCESS_RESPONSE);
                task = new UserLogin(s, serverLog);
                threadPool.execute(task);
                break;

            case DELETE_EMAIL:
                outputStream.write(SUCCESS_RESPONSE);
                task = new EmailDelete(s, serverLog);
                threadPool.execute(task);
                break;

            case NOTIFICATION:
                outputStream.write(SUCCESS_RESPONSE);
                task = new Notificator(s, serverLog);
                threadPool.execute(task);
                break;

            default:
                serverLog.writeLog("Errore di Richiesta");
                outputStream.write(FAILURE_RESPONSE);
                break;
        }
    }

package common;

public class ServiceID {

    public static final int WRITE_EMAIL = 84;
    public static final int READ_ALL_EMAILS = 50;
    public static final int FORWARD_EMAIL = 94;
    public static final int REPLY_EMAIL = 95;
    public static final int REPLY_ALL_EMAIL = 96;
    public static final int DELETE_EMAIL = 100;
    public static final int LOGIN = 11;
    public static final int USER_LIST = 111;
    public static final int SUCCESS_RESPONSE = 1;
    public static final int FAILURE_RESPONSE = -1;
    public static final int NOTIFICATION = 118;
    public static final int EMAIL_NOT_FOUND = 404;
    public static final int LOGOUT = 283;
}

these 2 pieces of code are the functions where I get the ServiceID from InputStream. The other class is all the possible serviceID number. Don't write : " you are probably pass serviceID wrongly", because this is not the case, all other ServiceID working properly.

If InputStream is java.io.InputStream , the return value is an 8-bit value (or -1), so it will not match 283.

public abstract int read() throws IOException

Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned.

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