Search code examples
javatelnet

Why is part of my Telnet program throwing an error about misplaced '}'?


I'm using BBEdit, and it seems to think that every '}' placed after the "hello" || "hi" if statement is unmatched, which obviously isn't true. The terminal also thinks this, as it throws an error about having a 'try' without a 'catch'. I was wondering if anyone has had the same issue and/or knows the solution?

Thanks in advance!

class client_handler extends Thread {
    private Socket conn;
    client_handler(Socket conn) {
        this.conn = conn;
    }
    public void run() {
        String line, input="";

        String username;
        String password;
        String UID;

        Map<String, String> database_UID = new HashMap<String, String>();
        database_UID.put("admin", "1234");

        try {
            //Fetches the socket reading and writing streams
            DataInputStream in = new DataInputStream(conn.getInputStream());
            PrintStream out = new PrintStream(conn.getOutputStream());

            //Sends a welcome message to new client
            out.println("Welcome to the server\nType your username and password like:\nUsername_Password");

            //Begins to read input from the client
            while((line = in.readLine()) != null && !line.equals(".")) {
                //The server replies with the same message
                if(line.equalsIgnoreCase("server_quit")) {
                    //Tells the server the command was given
                    System.out.println("Given command 'server_quit', closing client connection at "+conn.getPort());
                    //Tells the user that the server is closing
                    out.println("Quitting the server...\nHave a nice day!");
                    conn.close();
                }
                if(line.equals("admin_"+database_UID.get("admin") {
                    out.println("Welcome back, Admin!");
                }
                if(line.equalsIgnoreCase("hello") || line.equalsIgnoreCase("hi")) {
                    out.println("Good day!");
                }
            }
            System.out.println("Client terminated at "+conn.getPort());
            conn.close();
        }
        catch(IOException e) {
            System.out.println("IOException on socket: "+e);
            e.printStackTrace();
    }
}

Solution

  • Look at the brackets on the line above:

     if(line.equals("admin_"+database_UID.get("admin") {
    

    3 open, 1 close. That's not right. When checking this sort of thing (brackets, braces) start at zero. Add one for open, subtract one for close. If you don't end up with zero you've missed some (or added some if you end up negative).