Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By SeanCrowley2012

Thread: Simple Password

  1. #1
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Simple Password

    Hello, I am trying to make a simple class to make a password this is what I have

    Java Code:
    import java.util.Scanner;
    
    class Apples{
    	public static void main(String args[]){
    		Scanner input = new Scanner(System.in);
    		String attempt = input.nextLine();
    		String Password = "test";
    		
    		if (attempt == Password){
    			System.out.println("good");
    		}else{
    			System.out.println("error");
    		}
    		
    	}
    }
    But when I type in the password (test) it brings me to error.

    Thanks for any help

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Simple Password

    One problem with your code is that you're using == to check for String equivalence rather than use either the equals(...) or the equalsIgnoreCase(...) method. Understand that == checks if the two objects are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. So instead of

    Java Code:
    if (fu == "bar") {
      // do something
    }
    do,

    Java Code:
    if (fu.equals("bar")) {
      // do something
    }
    or,

    Java Code:
    if (fu.equalsIgnoreCase("bar")) {
      // do something
    }

  3. #3
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Simple Password

    Okay Thanks a lot! That makes more sense.
    Fubarable likes this.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. Simple Password Check problem
    By Gsangha in forum New To Java
    Replies: 1
    Last Post: 12-26-2011, 05:15 AM
  2. Simple password validation.
    By DarkAlex in forum New To Java
    Replies: 27
    Last Post: 03-21-2011, 01:28 AM
  3. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum New To Java
    Replies: 2
    Last Post: 11-14-2008, 07:53 PM
  4. how to check password for 3 times enterd wrong password
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 0
    Last Post: 11-14-2008, 01:22 PM
  5. How to check password of a jsp/html with the password of Database(mysql) #1
    By sk.mahaboobbhasha@gmail.c in forum Java Servlet
    Replies: 2
    Last Post: 11-14-2008, 01:11 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
  •