Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 strings and chars
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
person




PostPosted: Sun Oct 08, 2006 12:00 pm   Post subject: 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 [tmp] or something but I'm not so sure.

code:

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);
      }
  }
}
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Sun Oct 08, 2006 12:02 pm   Post subject: (No subject)

Strings are objects. Hence the capitalization of the type name. Within this simple fact lies the answer to your question.
[Gandalf]




PostPosted: Sun Oct 08, 2006 7:48 pm   Post subject: (No subject)

Expanding on what wtd said, once you know which class an object belongs to, you can look it up in the Java documentation, here. In this case, you would look up the String class and then look for the method that does what you are looking for. To compare Strings, you use the equals() method found in the String class. For example:
code:
if (str1.equals(str2)) System.out.println("equal");

as opposed to:
code:
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




PostPosted: Sun Oct 08, 2006 10:12 pm   Post subject: (No subject)

My post contained all the information needed to solve this problem. Wink
[Gandalf]




PostPosted: Sun Oct 08, 2006 10:50 pm   Post subject: (No subject)

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. Wink
wtd




PostPosted: Mon Oct 09, 2006 8:28 am   Post subject: (No subject)

[Gandalf] wrote:
"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. Wink
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: