Results 1 to 8 of 8
  1. #1
    fungui is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Question Help for basic password program

    Hi, i have started learning java, i'm not at all experienced.
    I decided to make a basic sort of password program, just for fun. It should print "access granted" if the password is correct, and "access denied" if not, but even if I type in the correct password, it prints "access denied". I am not sure why (by the way i'm using eclipse). The code is in two classes. Here they are (sorry, i used random names for variables and things):

    //class one, the main
    Java Code:
     import java.util.Scanner;
    public class main {
    	public static void main(String[] args) {
    		
    		Scanner bob = new Scanner(System.in);
    		
    		extra things = new extra();
    		
    		System.out.println("enter password");
    		
    		String pass = bob.nextLine();
    		
    		things.main(pass);
    		
    
    	}
    
    }
    
    //class two
    
    
    public class extra {
    	public void main(String pass) {
    		if (pass == "cheese"){
    			System.out.println("access granted");
    		} else {System.out.println("access denied")
    				;
    			
    		}
    	}
    
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: Help for basic password program

    When comparing Strings, don't use ==, but use equals().

    e.g

    Java Code:
    pass.equals("cheese")

    Also, you don't really need two classes for this. Just make the second class a method of the first.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: Help for basic password program

    You need one more right parentheses.

    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    fungui is offline Member
    Join Date
    May 2013
    Posts
    5
    Rep Power
    0

    Default Re: Help for basic password program

    Oh yeah, sorry for silly mistake, thanks, works now :D

  5. #5
    allaudin's Avatar
    allaudin is offline Senior Member
    Join Date
    Apr 2013
    Posts
    271
    Rep Power
    0

    Default Re: Help for basic password program

    pass == doesn't work for strings i think ...you should write pass.equal("cheese").

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,512
    Rep Power
    5

    Default Re: Help for basic password program

    allaudin,

    I understand you are trying to help but please read the entire post before responding. You repeated an answer (albeit incorrectly) that was provided a day earlier. It is equals() not equal(). You have also done this in several other threads.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    164
    Rep Power
    2

    Default Re: Help for basic password program

    When you are trying to see what a String equals in an if statement, don't use "==". This if for integers, doubles, and booleans. Use ".equals('text here')" instead.

  8. #8
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Help for basic password program

    To add a bit to what others have said: when it comes to objects, "==" tests if two variables refer to the same object (i.e., the same memory location). Sometimes this is actually what you want, but it usually isn't, so be careful.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Password Program Not Working Correctly
    By austin_gassert in forum New To Java
    Replies: 2
    Last Post: 07-21-2012, 06:12 PM
  2. Need help for password check program
    By AB1826 in forum New To Java
    Replies: 8
    Last Post: 02-16-2012, 05:19 AM
  3. my password program
    By zneith in forum New To Java
    Replies: 2
    Last Post: 01-12-2009, 05:14 AM
  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

Tags for this Thread

Posting Permissions

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