java code explanation

Nithya.P

Nithya.P

@nithyap-niSDb1 Oct 22, 2024
public String getUserID(String usernmae, String password) {
        String flag = "";
        try {
            if (true) {
                socket = new Socket("localhost", 8080);
                dos = new DataOutputStream(socket.getOutputStream());
                dos.writeUTF("SignIn");
                dos.writeUTF(username);
                dos.writeUTF(password);
                dis = new DataInputStream(socket.getInputStream());
                response = dis.readUTF();
                String[] res_arr = response.split("#");
                if (res_arr[0].equalsIgnoreCase("record")) {
                    flag = "success";
                    userid = Integer.parseInt(res_arr[1]);
                    port = Integer.parseInt(res_arr[2]);
                    secretkey = res_arr[3];
                } else {
                    flag = "failure";
                    // System.out.println("Login Failed...");
                }
 
            }

Can anyone please explain this code. This is just a part of the program.
After entering the user name and password it is going to "response = getUserID(username, password);"

I am not able to understand at the line String[] res_arr=response.split("#");

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • simplycoder

    simplycoder

    @simplycoder-NsBEdD May 17, 2013

    String[] res_arr=response.split("#");
    response is a string which ideally should contain "#" character used as a separator.
    and the in built split function, then would split the string into different strings based on the separator.
    Say response="simply#coder#is#a#crazyEngineer;
    then the response.split("#") would split the string whenever it encounters a "#" symbol and insert into String[]res serially.
    so res[0]="simply"
    res[1]="coder"
    res[2]="is"
    res[3]="a"
    res[4]="crazyEngineer"
    String[] res is a dynamic array.

  • Nithya.P

    Nithya.P

    @nithyap-niSDb1 May 17, 2013

    I too understood the same way. As I had mentioned before after submitting the username and password the String response = getUserID(username, password); is created. So thus response calls getUserID method. So from where does the # symbol come from for response string.