Results 1 to 5 of 5
  1. #1
    NextToNothing is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default Help Please - I'm confused

    Hello,
    Im trying to do something, which i would think, is simple. All i want to do if use if statements to check a string that is being split.
    The problem is, that i know that the string i am inputting should go straight through but it doesnt work at the first if statement.

    The code of the if statements is:
    Java Code:
            String[] packet = line.split("@",-1);
            if(packet[0].length() > 0) {
              if(packet[1]+1 == "n1") {
                String[] packet1 = packet[2].split("\\$\\$");
                if(packet1[1] == securityHash) {
                  String[] packet2 = packet1[0].split(":",-1);
                  if(packet2[0] != "" && packet2[1] != "") {
                    System.out.println("Packet Found to be Successful.");
                    textArea.append("Packet Found to be Sucessful.\n");
                    textArea.setCaretPosition(textArea.getDocument().getLength());
                    System.out.println("  Packet Command: "+packet2[0]);
                    textArea.append("  Packet Command: "+packet2[0]+"\n");
                    textArea.setCaretPosition(textArea.getDocument().getLength());
                    System.out.println("  Packet Server: "+packet2[1]);
                    textArea.append("  Packet Server: "+packet2[1]+"\n");
                    textArea.setCaretPosition(textArea.getDocument().getLength());
                    System.out.println("Preforming Packet Command.");
                    textArea.append("Preforming Packet Command.\n");
                    textArea.setCaretPosition(textArea.getDocument().getLength());
                    // Packet Successful
                  } else {
                    System.out.println("Packet Error: Invalid Action and/or Server.");
                    textArea.append("Packet Error: Invalid Action and/or Server.\n");
                    textArea.setCaretPosition(textArea.getDocument().getLength());
                  }
                } else {
                  System.out.println("Packet Error: Incorrect Security Hash.");
                  textArea.append("Packet Error: Incorrect Security Hash.\n");
                  textArea.setCaretPosition(textArea.getDocument().getLength());
                }
              } else {
                System.out.println("Packet Error: Unknown string configuration.");
                textArea.append("Packet Error: Unknown string configuration.\n");
                textArea.setCaretPosition(textArea.getDocument().getLength());
              }
            } else {
              System.out.println("Packet Error: Unknown string type.");
              textArea.append("Packet Error: Unknown string type.\n");
              textArea.setCaretPosition(textArea.getDocument().getLength());
            }
    The string is "line" and the string im entering is "@n@command:server$$hash" (without the quotes of course).
    Can someone point me in the right direction please.

    Thanks.

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    Hi. Just use "equals" operation for compare strings. Don't use ==. When you use == you check a hashCode this strings.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,406
    Rep Power
    25

    Default

    When you use == you check a hashCode this strings.
    The == operator tests if the contents of the two variables is the same. For objects that would be testing it the two object references/pointers refer to the same object. It wouldn't test the contents of the objects if the two references pointed to different objects. You use the equals method to test the contents of the referred to objects.

  4. #4
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    @Norm Of course I agree with you. I mean that this approach is used in map and other collections for determine where located the same variables. So It's very important to override equals and hashCode methods.
    Skype: petrarsentev
    http://TrackStudio.com

  5. #5
    NextToNothing is offline Member
    Join Date
    Jul 2011
    Posts
    2
    Rep Power
    0

    Default

    Thanks!
    You guys really help me :)

    Im using the .equals() now.
    :)

Similar Threads

  1. Confused :?
    By jESTPHROSTY in forum New To Java
    Replies: 2
    Last Post: 10-17-2010, 07:38 PM
  2. Help i am so confused..:(
    By angeltiner in forum AWT / Swing
    Replies: 6
    Last Post: 04-11-2010, 01:43 AM
  3. Very confused Plz help!!
    By ratb0y in forum NetBeans
    Replies: 0
    Last Post: 02-14-2009, 04:34 PM
  4. Confused
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 01-13-2009, 01:00 PM
  5. confused
    By updev in forum AWT / Swing
    Replies: 6
    Last Post: 11-14-2008, 03:33 PM

Posting Permissions

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