Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Comparing arrays if statement Help !

  1. #1
    steveday77 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Comparing arrays if statement Help !

    Hi i a having trouble with the code below, somewhere with my if statement im guessing i cannot get it to print out that the username and password are correct , i have checked that they are the same values by printing them out in this same method, namepassword[0] is "TOM" and "TOM" is in the list of userIDs being passed in from an array same with the passwords so i do believe there equal so i dont get why its not turning the boolean TRUE. Any advice would be greatly appreciated and i can post entire code if necessary.
    Thanks



    Java Code:
    public static void validation(String[] namepassword,String[] userId, String[] passwords)
    	{
    		int index = 0;
    		boolean found = false;
    		while(index < userId.length && !found)
    		{
    			if ((userId[index] == namepassword[0]) && (passwords[index] == namepassword[1]))
    				found = true;
    			else
    				index++;
    		}
    		String usergood = "The userId and password have been accepted";
    		String userbad = "The userId and password have been denied";
    		if (found)
    			println(usergood);
    		else
    			println(userbad);
    	}
    Last edited by steveday77; 04-21-2013 at 08:12 PM.

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

    Default Re: Comparing arrays if statement Help !

    You should use equals() and not == to compare Strings. Otherwise, you are only comparing the object references and not the String contents.

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

  3. #3
    steveday77 is offline Member
    Join Date
    Apr 2013
    Posts
    6
    Rep Power
    0

    Default Re: Comparing arrays if statement Help !

    Quote Originally Posted by jim829 View Post
    You should use equals() and not == to compare Strings. Otherwise, you are only comparing the object references and not the String contents.

    Regards,
    Jim
    Thanks Jim worked perfect.

Similar Threads

  1. comparing 2 arrays to see if they're equal
    By Get_tanked in forum New To Java
    Replies: 2
    Last Post: 02-17-2011, 06:59 AM
  2. Comparing arrays
    By mitty in forum New To Java
    Replies: 8
    Last Post: 04-14-2010, 11:55 AM
  3. Comparing two Char arrays
    By viperlasson in forum New To Java
    Replies: 3
    Last Post: 01-30-2010, 08:05 AM
  4. comparing arrays..
    By circuspeanuts in forum New To Java
    Replies: 5
    Last Post: 05-25-2009, 07:05 AM
  5. Replies: 2
    Last Post: 12-06-2008, 06:54 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
  •