Results 1 to 2 of 2

Thread: [JAVA] Unicode Characters and string copying/comparison

  1. #1

    [JAVA] Unicode Characters and string copying/comparison

    Hey all,
    I normally do C++ but lately ive been learning some Java, due to my CIT/TAFE course doing Java next semester.
    Anyway so ive been on www.net-force.nl doing some of the challenges they have over there, just to get to know Java better.
    NOTE: The terms and conditions of doing the challenges state that NO-ONE is to give me the answer/password, or help me on the challenges.
    so please DO NOT give me the answer/password.

    Im not asking for help on the challenge, but just some Java stuff that is used in the challenge.

    Code:
        public applet4()
        {
            SubmitButton = null;
            PasswordField = null;
            SubmitButton = null;
            PasswordField = null;
            SubmitButton = new Button();
            PasswordField = new TextField("", 8);
        }
    
        public void actionPerformed(ActionEvent actionevent)
        {
            String s = PasswordField.getText();
            String s1 = getDocumentBase().toString();
            char c = '\u01C0';
            int i = hash(s);
            s1 = s1.substring(0, s1.lastIndexOf('/') + 1);
            String s2;
            if(c == i)
                s2 = s1 + "index.php?pass=" + s;
            else
                s2 = s1 + "index.php?pass=kneus";
            try
            {
                getAppletContext().showDocument(new URL(s2), "_self");
            }
            catch(Exception exception)
            {
                exception.printStackTrace();
            }
        }
    
        private int hash(String s)
        {
            int i = 0;
            for(int j = 0; j < s.length(); j++)
                i += s.charAt(j);
    
            return i;
        }
    ok so thats part of the code.

    Q1
    The thing I would like to know is with this line of code:
    PasswordField = new TextField("", 8);
    then,
    String s = PasswordField.getText();

    Say I put "cats" in the password field, when the field value is copied into String s will String s then equal "cats" or will the rest of password field (8 characters long) be placed into it with other null characters or something? Like this "cats\0\0\0\0"?

    Q2
    As you can also see a comparison is made between char c and int i (return of hash(s))
    Code:
    if(c == i)
                s2 = s1 + "index.php?pass=" + s;
    and as you can also see, c = '\u01C0';
    So when the comparison is made "if(c == i)" will c be seen as '\u01C0' or the latin character 'ǀ'?
    And mainly, how the hell can you compare a character and an integer?!?!

    Im not sure if im even doing this the right way, but as i quoted at the start, no-one is to help with the challenge, or give out the answer/password!
    But your welcome to sign up and do it yourself (just dont show me up too much =P)

    I appreciate all the help you all always give me.
    Cheers.

  2. #2
    Q1
    You will just get "cats".

    Q2
    \uc01C0 is just an escape sequence which corresponds to '|'. So c simply contains '|' (as unicode).
    As chars are simply numbers in the range of [0,65535], you can simply compare them to ints.
    Last edited by Favardin; July 19th, 2008 at 03:38 AM. Reason: Forgot that java uses 2 bytes for characters.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012