3 Replies Latest reply: Mar 14, 2007 12:57 PM by 807606 RSS

    Problem in using URL Readers


      I have created some code, which is supposed to check a user's login by using a PHP page (can't get direct access to the database due to hosting restrictions).

      However, it always returns false. The way that it should check, is by looking up the page, then checking the pages return. The page always returns either valid or invalid. Part of the code works correctly, as the page returns valid, HOWEVER the program dosn't pick this up, and so returns false.

      This leads me to believe that there is an error in the part of the code which reads the page. I'm new to java, and created the page reading code from a tutorial, and so there may be an error in it. If anyone is able to identify where the error may be in the page reading code, i'd be greatful.

      The code for it is:
          private void logincheck(java.awt.event.ActionEvent evt) {                            
              try {
                  char[] PasswordInput = Password.getPassword();
                  String PasswordRaw = new String(PasswordInput);
                  String Passwordmd5 = md5(PasswordRaw);
                  String UsernameInput = Username.getText();
                  URL theUrl = new URL("http://www.phoenixrising.at/iris/validate.php?login=1&u=" + UsernameInput + "&p=" + Passwordmd5 +"");
                  BufferedReader in = new BufferedReader(
                          new InputStreamReader(
                  String inputLine = in.readLine();
                  if (inputLine == "valid") {
                      System.out.println( "You have entered the correct password" );
                  } else {
              } catch (MalformedURLException ex) {
              } catch (IOException ex) {
        • 1. Re: Problem in using URL Readers
          You're using the '=='operator to check for string equality. Try using the String's equals() method instead.

          if (inputLine == "valid")
          should be
          if (inputLine.equals("valid"))
          The '==' operator will only check to see if the two variables are referencing the same memory on the heap. In your case, it is always going to be false.
          • 2. Re: Problem in using URL Readers
            Ah thankyou for the help, now works perfectly.

            (Dukes going to kevjava).

            I am curious as to the different between the two though, when should you use == and when should you use the String's equals() method?

            • 3. Re: Problem in using URL Readers
              For any primitive type, you want to use the == method. For objects, you want to use the .equals() method.
              public class Dog {
                private String name = "";
                public String getName() { return name; }
                public void setName(String name) { this.name = name; }
                public Dog(String name) { this.name = name; }
                public void bark() { System.out.println("\"Arf\", says " + name + "."); }
                public boolean equals(Dog otherDog) {
                  return (this.name.equals(otherDog.getName()));
                public static void main(String [] args) {
                  Dog jim = new Dog("jim");
                  Dog bob = new Dog("bob");
                  Dog likeBob = new Dog("bob");
                  Dog otherJim = jim; // Notice no "new", thus it's referencing the same Dog.
                  System.out.println( bob.equals(jim) ); // false, because their names are different.
                  System.out.println( bob.equals(likeBob) ); // true
                  likeBob.bark();  // prints "Arf", says bob.
                  System.out.println( bob == likeBob ); // false, because they're two different dogs.
                  otherJim.bark();  // prints "Arf", says jim.
                  System.out.println( otherJim == jim ); // true, because they reference the same dog.
              Hope that helps.