3 Replies - 491 Views - Last Post: 25 May 2012 - 09:31 AM Rate Topic: -----

#1 GilkeAnthony  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 14-May 12

Java Compare Strings Not Working

Posted 25 May 2012 - 08:01 AM

I'm trying to compare a string returned from a hashmap to a JTextField. I'm able to print them both and they ARE EQUAL, but when I run the following code they do not show as equal.

	btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(textFieldUsername.getText().toString() == myString.toString()){
					System.out.println("For the usernamename: " + "<" +nameString.toString() + ">" + " your login was Successful!");		
					lblPic.setVisible(true);
					
				}
				
				else{		
					
					System.out.println("For the usernamename: " + "<" +nameString.toString() + ">" + " your login was Incorrect!");	
					lblPic_1.setVisible(true);
					
					System.out.println("test: " + textFieldUsername.getText() + nameString);
					
				
					}
			}
		});
	



As you can see where I have "System.out.println("test: " + textFieldUsername.getText() + nameString);"

The results are the exact same.


Here is my whole code.

package HashMapFun;

import java.awt.BorderLayout;

public class HashMapFunSwing extends JFrame {

	private JPanel contentPane;
	private final JButton btnYes = new JButton("Retrieve Login");
	private final JButton btnNo = new JButton("Exit");
	private final JLabel lblEnterYourName = new JLabel("Enter Your Name to Recieve Secure Login:");
	private final JTextField textFieldEnterName = new JTextField();
	private final JButton btnSecureLogin = new JButton("SECURE LOGIN");
	private final JLabel lblCodeResults = new JLabel("Code");
	int hash = 0;
    String myString;
    Hashtable ht = new Hashtable();
    String resultName = myString;
	String name = textFieldEnterName.getText();
	private final Panel LoginPanel = new Panel();
	private final JTextField textFieldUsername = new JTextField();
	private final JLabel lblEnterUsername = new JLabel("Enter Username: ");
	private final JLabel lblEnterPassword = new JLabel("Enter Password:");
	private final JTextField textFieldPassword = new JTextField();
	private final JButton btnLogin = new JButton("Login");
	private final JLabel lblPic = new JLabel(" ");
	String nameString;
    ImageIcon sheen = new ImageIcon("resourcess/sheen.jpg");
    ImageIcon bike = new ImageIcon("resourcess/bike.jpg");
    private final JLabel lblPic_1 = new JLabel(" ");
    private final JLabel lblPlease = new JLabel("Please");
    

	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HashMapFunSwing frame = new HashMapFunSwing();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public HashMapFunSwing() {
		textFieldEnterName.setBounds(297, 8, 169, 20);
		textFieldEnterName.setColumns(10);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 550, 492);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		btnNo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		textFieldPassword.setBounds(170, 59, 216, 20);
		textFieldPassword.setColumns(10);
		textFieldUsername.setBounds(170, 11, 216, 20);
		textFieldUsername.setColumns(10);
		LoginPanel.setBounds(0, 0, 534, 409);
		LoginPanel.setVisible(false);
		
		contentPane.add(LoginPanel);
		LoginPanel.setLayout(null);
		
		LoginPanel.add(textFieldUsername);
		lblEnterUsername.setBounds(28, 14, 132, 14);
		
		LoginPanel.add(lblEnterUsername);
		lblEnterPassword.setBounds(28, 62, 132, 14);
		
		LoginPanel.add(lblEnterPassword);
		
		LoginPanel.add(textFieldPassword);
		
		LoginPanel.add(btnLogin);
		
		btnNo.setBounds(445, 415, 89, 23);
		
		contentPane.add(btnNo);
		btnYes.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
								
				String name = textFieldEnterName.getText();
				
				hash = HashMapFun.Demo(name);	
								
		        HashMapFun.HashMe(name, "SHA-1");
				            
		        ht.put(hash, name);
		
		        myString = String.valueOf(ht.get(hash));
			        
			    lblCodeResults.setText("Your Username is: " + "<" + myString + ">" + " Your Password is: " +"<" + hash + ">");
			    lblPlease.setText("Please keep Username and Password in Secure Location.");
				  
			    lblCodeResults.setVisible(true);	
			    lblPlease.setVisible(true);
			    //		      get() returns an object. We need to convert it to a string.
		        nameString = String.valueOf(ht.get(hash));
		        System.out.println("String retrieved from hash table = <" + nameString + ">");

			    
			}
		});
		btnYes.setBounds(168, 80, 186, 23);
		
		contentPane.add(btnYes);
		lblEnterYourName.setBounds(10, 11, 277, 14);
		
		
		contentPane.add(lblEnterYourName);
		
		contentPane.add(textFieldEnterName);
		btnSecureLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				LoginPanel.setVisible(true);
				
				
			}
		});
		
		btnLogin.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				if(textFieldUsername.getText().toString() == myString.toString()){
					System.out.println("For the usernamename: " + "<" +nameString.toString() + ">" + " your login was Successful!");		
					lblPic.setVisible(true);
					
				}
				
				else{		
					
					System.out.println("For the usernamename: " + "<" +nameString.toString() + ">" + " your login was Incorrect!");	
					lblPic_1.setVisible(true);
					
					System.out.println("test: " + textFieldUsername.getText() + nameString);
//					System.out.println(nameString + myString);
					
				
					}
			}
		});
		
		btnLogin.setBounds(417, 33, 89, 23);
		
		lblPic.setBounds(56, 87, 478, 311);

		lblPic_1.setBounds(28, 89, 459, 320);
		
		LoginPanel.add(lblPic);
		
		LoginPanel.add(lblPic_1);
			
		lblPic.setIcon(sheen);
		
		lblPic_1.setIcon(bike);
	
		lblPic_1.setVisible(false);	
		
		lblPic.setVisible(false);
		
		btnSecureLogin.setBounds(183, 216, 154, 23);
		
		contentPane.add(btnSecureLogin);
		lblCodeResults.setBounds(8, 411, 427, 14);
		
		contentPane.add(lblCodeResults);
		lblPlease.setBounds(10, 429, 383, 14);
		
		contentPane.add(lblPlease);
		
		lblCodeResults.setVisible(false);
		lblPlease.setVisible(false);
		
	}
}





Hash map area:


package HashMapFun;

/*
Java HashMap example.
This Java HashMap example describes the basic operations performed on the HashMap.
*/
 
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Iterator;
 
public class HashMapFun{
	  public static void HashMe(String sessionID, String Encryption) {

	        byte[] defaultBytes = sessionID.getBytes();
	        try {
	            MessageDigest algorithm = MessageDigest.getInstance(Encryption);
	            algorithm.reset();
	            algorithm.update(defaultBytes);
	            byte messageDigest[] = algorithm.digest();

	            StringBuffer hexString = new StringBuffer();
	            for (int i = 0; i < messageDigest.length; i++) {
	                hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	            }
	            String foo = messageDigest.toString();
//	          These days, < and > are not good choices for delimiters
	            System.out.println("Your Username is: <" + sessionID + "> Your Password encrypted with " + Encryption + " is <" + hexString.toString() + ">");
	            sessionID = hexString + "";
	        } catch (NoSuchAlgorithmException nsae) {
	        }
	        
	}
	  public static int Demo(String stringToBeHashed)
	    {
	        int hashResult;
	        hashResult = stringToBeHashed.hashCode();

	        return hashResult;
	    }
	}
       
 





Is This A Good Question/Topic? 0
  • +

Replies To: Java Compare Strings Not Working

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2657
  • View blog
  • Posts: 11,207
  • Joined: 20-September 08

Re: Java Compare Strings Not Working

Posted 25 May 2012 - 08:11 AM

You can't compare String content with ==. Use String.equals

if(textFieldUsername.getText().toString().equals(myString.toString())){


btw, you don't need thw first toString call and maybe not the second either

This post has been edited by g00se: 25 May 2012 - 08:13 AM

Was This Post Helpful? 1
  • +
  • -

#3 GilkeAnthony  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 14-May 12

Re: Java Compare Strings Not Working

Posted 25 May 2012 - 09:27 AM

View Postg00se, on 25 May 2012 - 08:11 AM, said:

You can't compare String content with ==. Use String.equals

if(textFieldUsername.getText().toString().equals(myString.toString())){


btw, you don't need thw first toString call and maybe not the second either


That makes sense, I wonder how to compare strings a different way? Thanks!
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: Java Compare Strings Not Working

Posted 25 May 2012 - 09:31 AM

The only other method would be to use .compareTo. If it returns 0 then the Strings are equal -

if(string1.compareTo(string2) == 0) // if they are equal
{
 // do something
}


But as you can see the .equals is just easier.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1