java code explanation

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

  • simplycoder
    simplycoder
    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
    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.

You are reading an archived discussion.

Related Posts

Ever since Marissa Mayer took over the control at Yahoo, the company's managed to grab some attention from the media. This time, it's about Yahoo's plans to buy Tumblr -...
Google's launched 'Native Client' a new technology that will allow developers to run their native C, C++ compiled code on the web; as a part of the web apps. Native...
What is a dangling name and Dangling Pointer????
can u explain me clearly about automation in transmission substations ????
Greetings Readers, Here is the first article in my series "A Walk to Secure future" "Let us discuss about the basic concept of hacking and general notions and knowledge of...