CrazyEngineers
  • java code explanation

    Nithya.P

    Member

    Updated: Oct 22, 2024
    Views: 1.3K
    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("#");
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • simplycoder

    MemberMay 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.
    Are you sure? This action cannot be undone.
    Cancel
  • Nithya.P

    MemberMay 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.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register