Results 1 to 12 of 12
  1. #1
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default Equals doesn't equal!

    This is just weird. The code in bold is a boolean that measures whether or not the String parameter and the String of the method are the same. I have tried using equals, startsWith and contains methods and all keep returning false when its true! Using my debugger, I've confirmed that on the 2nd iteration (when counter = 1) that both value and compare are MAT350. I know it. Eclipse knows it (even assigns both variables id = 37.) So why doesn't java know it?!?!?!? The chunk of code at the bottom is from the Junit test case.

    Java Code:
    public class CourseList implements DiaryItemList {
    	private static LinkedNode head;
    	
    	private class LinkedNode {
    		private String name;
    		private String daystimes;
    		public DiaryItem data;
    		public LinkedNode link;
    		
    		public LinkedNode(Course o, LinkedNode link) {
    			data = o;
    			this.link = link;
    			if(o != null){
    			this.name = o.getName();
    			this.daystimes = o.getDaysTimes();
    			}
    		}
    	}
    	
    public CourseList() {
    		head = null;
    	}
    	public CourseList(String x){
    		 try {
    			   Course temp = new Course();
    			   FileReader from = new FileReader(x);
    			   BufferedReader input = new BufferedReader(from);
    			   String courseinfo = input.readLine();
    			   StringTokenizer cl = new StringTokenizer(courseinfo);
    			   
    			   String coname = cl.nextToken();
    			   temp.setName(coname);
    			   
    			   temp.setSection(cl.nextToken());
    			   String startT = cl.nextToken();
    			   startT = startT.replaceAll(":","");		
    			   startT.trim();
    			   int starter = temp.toMilitaryTime(startT);
    			   if(starter >= 1200){
    			    		startT = "" + startT + "PM";
    			   }
    			   else {
    			    		startT = "" + startT + "AM";
    			   }
    			   temp.setStartTime(starter);
    			   String endT = cl.nextToken();
    			   endT = endT.replaceAll(":","");
    			   endT.trim();
    			   int ender =temp.toMilitaryTime(endT);
    			   	if(ender >= 1200){
    			    	 endT = "" + endT + "PM";
    			   	}
    			   	else {
    			    		endT = "" + endT + "AM";
    			   	}
    			   temp.setEndTime(ender);
    			   String dayconvertor = cl.nextToken();
    			   dayconvertor = dayconvertor.replace('H','Y');
    			   dayconvertor = dayconvertor.replace('F','Z');
    			   temp.setDays(dayconvertor);
    			   courseinfo = input.readLine();
    			   insertAtFront(temp);
    			   System.out.print(temp);  
    		   }
    		   catch (FileNotFoundException e) {
    				System.out.println("File classy.txt not found");
    		   }
    		   catch(IOException e) {
    				System.err.println("Exception in reading from file");
    		   }	
    	}
    
    	public DiaryItem getItemAt(int index) {
    		LinkedNode p = head;
    		if(p == null)
    			return null;
    		int counter = 0;
    		while (p != null && counter != index){
    			p = p.link;
    			counter++;
    			}
    		if(counter == index){
    			return (DiaryItem) p.data;
    		}
    		
    		return null;
    	}	
    
    		public int indexOf(String value) {
    		
    		LinkedNode current = head;
    		LinkedNode previous = null;
    		String compare = current.data.getName();
    		[B][U]boolean match = compare.startsWith(value);[/U][/B]
    		int counter = 0;
    		while(current != null && match == false) {
    			previous = current;
    			current = current.link;
    			compare = current.data.getName();
    			counter++;
    		}	
    		if(current != null && match == true) {
    			return counter;
    			}
    
    		return counter;
    	}
    public void testIndexOf() {
    		String Beta = B.getName();  // String = "MAT350"
    		myList.addInNameOrder(A);
    		myList.addInNameOrder(B);
    		myList.addInNameOrder(E);
    		int b = myList.indexOf(Beta);
    		assertEquals("index of b should equal ", 1, b);
    Last edited by MetalR0; 08-03-2011 at 04:54 AM.

  2. #2
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    what is the code below supposed to be doing? It appears that the match variable should be updating to break the loop.
    Java Code:
    	while(current != null && match == false) {
    			previous = current;
    			current = current.link;
    			compare = current.data.getName();
    			counter++;
    		}	
    		if(current != null && match == true) {
    			return counter;
    			}
    
    		return counter;

  3. #3
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    You summed it up. The code should iterate through the declared objects until one with a name matching the parameter value is hit. The match boolean should then change to true and break the loop.

  4. #4
    yellowledbet is offline Senior Member
    Join Date
    Feb 2011
    Location
    Georgia, USA
    Posts
    122
    Rep Power
    0

    Default

    where within the loop are you comparing 'name matching value' and where are you updating match to break the loop? I think that is the problem

  5. #5
    MetalR0 is offline Member
    Join Date
    Jul 2011
    Posts
    23
    Rep Power
    0

    Default

    Oh I thought the boolean would just update on its own once the condition became true. Now I've got it. Thanks!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    Also, never compare booleans to true/false.
    Java Code:
    // while(current != null && match == false) {
    while(current != null && !match) {
    :
    :
    }	
    // if(current != null && match == true) {
    if(current != null && match) {
    db

  7. #7
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default

    why we can't compare boolean to true / false?

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    You can, but it's generalyl a sign you don't know what a boolean is.
    Comparing one to true or false is like saying:
    Java Code:
    if (true == true) {
    }
    Which is pointless.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,303
    Rep Power
    20

    Default

    Why would you want to? and where would you stop?
    Java Code:
    boolean b = true;
    if (b) System.out.println("b is true.");
    if (b == true) System.out.println("b is true.");
    if ((b == true) == true) System.out.println("b is true.");
    if (((b == true) == true) == true) System.out.println("b is true.");
    :
    :
    if ((((((((((b == true) == true) == true) == true) == true) == true) == true) == true) == true) == true) System.out.println("b is true.");
    Another very good reason to avoid the redundant '== true / false' is that is is possible to mistakenly omit one = resulting in an assignment instead of a comparison operator.
    Java Code:
    if (b = true) System.out.println("b is true now, regardless of the value it held before this line.");
    db

  10. #10
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Yea but if you want to do
    boolean b=//anything;
    if (b==false) { System.out.println("b is false");}



    Quote Originally Posted by Tolls View Post
    You can, but it's generalyl a sign you don't know what a boolean is.
    Comparing one to true or false is like saying:
    Java Code:
    if (true == true) {
    }
    Which is pointless.
    it's not like saying true==true .. this will get u a true just as when u say false==false..
    true==false and false==true will get u a false..
    I dont see the problem of using it

  11. #11
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,044
    Rep Power
    20

    Default

    Because it should be:
    Java Code:
    if (!b) {
        System.out.println("b is false");
    }
    Anything else is redundant, and a good code analyser (which a lot of IDEs come with now) will rightly point it out to you.
    Redundant code just clogs up your source.

  12. #12
    Rami is offline Member
    Join Date
    Jul 2011
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Because it should be:
    Java Code:
    if (!b) {
        System.out.println("b is false");
    }
    Anything else is redundant, and a good code analyser (which a lot of IDEs come with now) will rightly point it out to you.
    Redundant code just clogs up your source.
    Convinced me with this one!

Similar Threads

  1. <logic:equal> tag
    By satyaprsp in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 08-18-2010, 07:52 AM
  2. help with not equal to operator !=
    By manowar689 in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 12:10 AM
  3. equal() method
    By need_helpp in forum New To Java
    Replies: 3
    Last Post: 03-09-2010, 05:57 PM
  4. ComparisonFailure on equal(to the eye) strings
    By staffan in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 02:57 PM
  5. Replies: 0
    Last Post: 01-25-2008, 11:13 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
  •