Results 1 to 12 of 12
Thread: Equals doesn't equal!
- 08-03-2011, 04:52 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
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.
- 08-03-2011, 05:09 AM #2
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
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;
- 08-03-2011, 05:16 AM #3
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
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.
- 08-03-2011, 05:21 AM #4
Senior Member
- Join Date
- Feb 2011
- Location
- Georgia, USA
- Posts
- 122
- Rep Power
- 0
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
- 08-03-2011, 05:27 AM #5
Member
- Join Date
- Jul 2011
- Posts
- 23
- Rep Power
- 0
Oh I thought the boolean would just update on its own once the condition became true. Now I've got it. Thanks!
- 08-03-2011, 07:21 AM #6
Also, never compare booleans to true/false.
dbJava Code:// while(current != null && match == false) { while(current != null && !match) { : : } // if(current != null && match == true) { if(current != null && match) {
- 08-03-2011, 09:05 AM #7
why we can't compare boolean to true / false?
- 08-03-2011, 09:57 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
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:
Which is pointless.Java Code:if (true == true) { }
- 08-03-2011, 10:03 AM #9
Why would you want to? and where would you stop?
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: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.");dbJava Code:if (b = true) System.out.println("b is true now, regardless of the value it held before this line.");
- 08-03-2011, 01:30 PM #10
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
Yea but if you want to do
boolean b=//anything;
if (b==false) { System.out.println("b is false");}
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
- 08-03-2011, 01:54 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Because it should be:
Anything else is redundant, and a good code analyser (which a lot of IDEs come with now) will rightly point it out to you.Java Code:if (!b) { System.out.println("b is false"); }
Redundant code just clogs up your source.
- 08-03-2011, 02:04 PM #12
Member
- Join Date
- Jul 2011
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
<logic:equal> tag
By satyaprsp in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 08-18-2010, 07:52 AM -
help with not equal to operator !=
By manowar689 in forum New To JavaReplies: 9Last Post: 06-15-2010, 12:10 AM -
equal() method
By need_helpp in forum New To JavaReplies: 3Last Post: 03-09-2010, 05:57 PM -
ComparisonFailure on equal(to the eye) strings
By staffan in forum New To JavaReplies: 2Last Post: 03-12-2009, 02:57 PM -
name clash: equals(E) in and equals(java.lang.Object)
By AdRock in forum New To JavaReplies: 0Last Post: 01-25-2008, 11:13 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks