
-----------------------------------
person
Sun Oct 08, 2006 12:00 pm

strings and chars
-----------------------------------
How do I make the conditional statement return true?
I think it may be due to the fact that I'm adding chars to 
public class Test
{
  public static void main (String [] args) throws IOException 
  {
    String str = "abc";
    String tmp = "";
    

      for (int i = 0 ; i < str.length (); i++)
      {
       tmp += str.charAt (i);
      }

      if (str == tmp)
      {
       System.out.println (str); 
      }
  } 
}


-----------------------------------
wtd
Sun Oct 08, 2006 12:02 pm


-----------------------------------
Strings are objects.  Hence the capitalization of the type name.  Within this simple fact lies the answer to your question.

-----------------------------------
[Gandalf]
Sun Oct 08, 2006 7:48 pm


-----------------------------------
Expanding on what wtd said, once you know which class an object belongs to, you can look it up in the Java documentation, if (str1.equals(str2)) System.out.println("equal");
as opposed to:
if (str1 == str2) System.out.println("equal");
The explanation on why you must do this is a bit more complicated though, and would require another post.

-----------------------------------
wtd
Sun Oct 08, 2006 10:12 pm


-----------------------------------
My post contained all the information needed to solve this problem.  ;)

-----------------------------------
[Gandalf]
Sun Oct 08, 2006 10:50 pm


-----------------------------------
Hrm...  I had a feeling you'd say that.  So instead of writing some huge post poorly explaining why I posted that, I'll try to put it concisely.  

I disagree. Well, I agree, but that information is buried behind tons and tons of other information.  Not everyone has the initiative to find it, and most people will just post again asking "what's an object?  what's a class?  where do I find the java documentation?  how do I use it?" and so on.  So instead of filling up an already spammed forum with those questions, I answer them before they are asked.  

You have an approach to teaching that I've seen few other people have, it won't work for everyone though. ;)

-----------------------------------
wtd
Mon Oct 09, 2006 8:28 am


-----------------------------------
"]"what's an object?  what's a class?  where do I find the java documentation?  how do I use it?"

These questions are all central to programming in Java.  Might as well ask them sooner rather than later.  ;)
