View Single Post
  #4 (permalink)  
Old 08-07-2007, 06:19 AM
cachi cachi is offline
Member
 
Join Date: Jul 2007
Posts: 40
cachi is on a distinguished road
It is your if statements checking if a list is "".

In Java a String is treated as an object. Since it is not a primitive type (like an int, float, double, char), you cannot do boolean comparisons such as == != >, etc.

Instead you have to use something in the String class called equals or equalsIgnoreCase.

Both these method take in a string as a parameter and return true if that String is the same as the string it is being called upon. For example
Code:
String test = "test" if(test == "test") System.out.println("TEST WORKED");
The above code may work once, maybe a couple times. However most likely it wont. It wont print anything. Why? Because since Strings are treated as Objects, they are used just like Objects. That is they are referenced. When you do an == you are checking if the first String test is the same reference as the String "test". NOT if they are the same String.

The correct way:
Code:
if(test.equals("test")) System.out.println("String comparison worked!");
Will return true every time and print every time.

Sorry if this seems spaced out. I am working off of a full night of no sleep after programming in Lisp.
Greetings.
Reply With Quote