Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Password Application I'm Coding Won't Work

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Password Application I'm Coding Won't Work

    I'm trying to make an application where you register a username and password then login, but when for some reason it won't recognize that the password is correct. Here is my code.

    import javax.swing.JOptionPane;
     
    public class Main {
    	String username;
    	String password;
     
    	public void start(){		
    		int a = JOptionPane.showConfirmDialog(null, "Do you have an account?", "Login / Register", 1);
     
    		if(a == JOptionPane.NO_OPTION){
    			regAccount();
    		}
    		if(a == JOptionPane.YES_OPTION){
    			login();
    		}
     
    	}
     
    	public void login(){
    		String loginName = JOptionPane.showInputDialog("Please Enter Your Username");
    		String loginPass = JOptionPane.showInputDialog("Please Enter Your Password");
     
     
     
    		if(loginName == username){
    			JOptionPane.showMessageDialog(null, "Login succesful");
    		}else{
    			JOptionPane.showMessageDialog(null, "Login failed");
    		}
    	}
     
    	public void regAccount(){
    		username = JOptionPane.showInputDialog("Please Enter a Username:");
    		password = JOptionPane.showInputDialog("Please Enter a Password");
     
    		JOptionPane.showMessageDialog(null, "Your username is " +username, "Register", JOptionPane.PLAIN_MESSAGE);
    		JOptionPane.showMessageDialog(null, "Your password is " +password, "Register", JOptionPane.PLAIN_MESSAGE);
    		start();
    	}
     
    	public static void main(String[] args){
    		Main main = new Main();
    		main.start();
     
    	}
    }

    Sorry if my code is messy.


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Password Application I'm Coding Won't Work

    Your code doesn't look messy to me, and in fact seems pretty well formatted.

    I see one problem in that you're comparing Strings using == which you should almost never do since == checks to see if one object reference is the exact same as another, and you really don't care about this. You don't want to know if one String object is the same as another String object, but instead you want to make sure that the two String objects hold the same characters in the same order, and possibly with the same capitalization. For that you should use the method equals(...) or equalsIgnoreCase(...).

    so instead of
    if (oneString == anotherString) {
       // ...
    }

    do something like
    if (oneString.equalsIgnoreCase(anotherString)) {
       // ...
    }

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Password Application I'm Coding Won't Work

    Thanks! It works fine now.

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Password Application I'm Coding Won't Work

    Great, glad you've got it working!

Similar Threads

  1. Image won't work!
    By mkrage in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 26th, 2012, 05:08 PM
  2. Why won't this for loop work?
    By vwillis in forum Loops & Control Statements
    Replies: 1
    Last Post: October 14th, 2011, 12:49 PM
  3. MergeSort won't work
    By joshft91 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 09:44 PM
  4. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM