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 5 of 5

Thread: Boolean Method Test Problem

  1. #1
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Boolean Method Test Problem

    Hi, I am having troube with returning a boolean

    	public boolean check(String name, String date){
     
    		boolean test = false;
     
    		for(int i=0; i<myList.size(); i++){
    			System.out.println("TEST: : " + "sent in name is " + name + "and stored name is" + myList.get(i).fileName);
    			System.out.println("TEST: : " + "sent in date is " + date + "and stored date is" + myList.get(i).fileDate);
     
     
    			if(name == myList.get(i).fileName && date == myList.get(i).fileDate){
    				test = true;		// if found then return true
    			}
    			else{
    				test = false; 	// if not found then return false
    			}
    		}
     
    		System.out.println("TEST IS " + test);
     
    		return test;
    	}

    this always returns false even though when i print out the lines to test I see that the name sent in and stored are the same, and the date sent in and stored are the same

    i dont understand why it dosnt return true or false depending on the check that is made.

    Thanks


  2. #2
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Boolean Method Test Problem

    name == myList.get(i).fileName
    Don't use == to compare strings or other objects, use their equals() method instead.

    if(name.equals(myList.get(i).filename) // etc

    -----

    == tests two references to see if they refer to the same thing. That's not the same thing as testing whether the things they refer to (the strings) have the same characters in the same order which is what .equals() does.

  3. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Khadafi (January 13th, 2012)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Boolean Method Test Problem

    == vs equals....when comparing objects, use equals. See the following for more information

    http://www.javaprogrammingforums.com...html#post18725

    edit: doh. Beaten to the punch.

  5. The Following User Says Thank You to copeg For This Useful Post:

    Khadafi (January 13th, 2012)

  6. #4
    Member
    Join Date
    Dec 2011
    Posts
    38
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Boolean Method Test Problem

    oo yea I remember hearing that before.

    Thanks for that =), works great.

  7. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Boolean Method Test Problem

    You're welcome.

Similar Threads

  1. Boolean method issue
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 23rd, 2011, 11:26 PM
  2. Method Equals, Boolean...
    By boys8goods in forum Object Oriented Programming
    Replies: 1
    Last Post: June 25th, 2011, 03:34 PM
  3. Need help with boolean method! =(
    By leao in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 14th, 2010, 10:18 AM
  4. Boolean method returning a boolean value
    By Deprogrammer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 21st, 2010, 10:56 AM
  5. Method returning boolean
    By Plural in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2010, 06:45 PM