Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Java (http://www.programmingforums.org/forum17.html)
-   -   Comparing strings.... (http://www.programmingforums.org/showthread.php?t=2927)

balltheheed Mar 25th, 2005 11:25 AM

Comparing strings....
 
Hi everyone,
I have a small problem that I just can not work out, regarding comparing user input and pre defined variables. For some reason it just doesn't want to work!!

:

btnSubmit.addActionListener
        (
                new ActionListener()
                {
                        //perform an action if button is pressed
                        public void actionPerformed(ActionEvent e)
                        {
                                String enteredUsername = txtUsername.getText();
                                char[] enteredPassword = txtPassword.getPassword();
                                String stringToPrint = new String (enteredPassword);
                                       
                                if (enteredUsername == username && stringToPrint == password)
                                {
                                        //constructor to load next window
                                        mainUI loginNick = new mainUI();
                                       
                                        //close the window onjce the person is logged in
                                        dispose();
                                }
                                       
                                else
                                {
                                        System.out.println("Enter your bloody details correctly");
                                }

                        }
                }
        );


Can someone please tell me what I am doing wrong?

Ps System.out's show they are the same!? :confused:

xavier Mar 25th, 2005 12:21 PM

:

if (enteredUsername.equals("username") && stringToPrint.equals("password"))

you can't compare to strings the same way as you do with integers. You have to use equals()

Java_Student Mar 25th, 2005 2:12 PM

you can also use equalsIgnoreCase() if you don't want the username and password to be case sensitive

Gigabytee Apr 8th, 2005 4:26 AM

Extra Information
 
For more Information on Interesting methods of the class string go to:
Java API: String
------
Gigabytee


All times are GMT -5. The time now is 7:48 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC