Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Censoring Words - Using String Equality

looking for code

I still don't understand on how to know and read what code you need when you search in the internet. For example in this course, instructor was looking for strings for blocking some words. In the video he mention about equal string and boolean. How do you come up with those conclusion?

3 Answers

Mostly they are trying to show you that the documentation is one of your best learning sources. Languages have a lot of function to them and you are not going to know all of them. Java is also a beast of of a language so you picked a challenging but powerful starting point.

Though you may not know all the functions you will start to pickup on the general layout and logic of programming in general. Then you can take that and find what you need.

In this case he took the fact that we wanted to do something(block the word) 'if' a condition(the word 'equals' the blocked word) is 'true'(Boolean). Since you are working with a string he looked up the functions you can preformed on a string and then looked through the documentation to see how to compare it to another.

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Caroline;

Excellent question. Andrew VanVlack gave a very nice answer.

I would add as well that, for myself, there are often times when working through a project that some of the code in which I write seems to be "clunky" and way too verbose for what seems to be a simple concept. Much of the time I think that it is a concept that many people would have needed at some point as well. So, off to the search engines I go.

Often I discover that there is already a function I was unaware of that does what I want to do in a specific language. Other times I find helpful resources that show a more simplified way of doing what I am trying to do. Either way, my learning has just increased which makes me more efficient. Both great things.

In terms of the videos and courses here on Treehouse, as Andrew mentioned, much of the time the instructors are showing where to look and the method(s) in which they use to look for things. Programming languages are powerful things, with lots of syntax involved. Knowing all of it about a single language is difficult, knowing where to find it is priceless. Add to that the fact that many employers want you to know multiple languages, and search engines become your friend and knowing where and how to look for things in them is crucial.

Hope it helps and welcome to Treehouse!

Ken

I'm having a similar issue.

Can someone please explain to me how you get from

"public boolean equals(Object anObject)

Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

Overrides: equals in class Object Parameters: anObject - The object to compare this String against Returns: true if the given object represents a String equivalent to this string, false otherwise See Also: compareTo(String), equalsIgnoreCase(String)"

to the following:

      if (noun.equals("dork")) {
       console.printf("That language is not allowed. Exiting. \n\n");
       System.exit(0); 
      }

I just want to know how to interpret this kind of information.

Thanks in advance.

Max.

Harry James
Harry James
14,780 Points

Hey Max!

Sure thing! I'll try to explain it the best I can and if you have any more questions after that, give me a shout :)


From the documentation, we can see that this is a boolean. A boolean value returns either true/false. The way if statements work is that if the value in the parentheses is true, the if statement will run. If it's false, it will move to the else statement (If supplied) or if not supplied, it will just carry on running the code beneath it.

Here's an example:

if (true) {
   console.printf("I will print to the console!");
}
// but, this won't work:
if (false) {
   console.printf("I will be ignored!");
}

// and, if you want to be even more advanced,

if (false) {
   console.printf("I will be ignored!");
}
else {
   console.printf("I will print to the console!");
}

That's enough about if statements anyway! Back to the method!

We can see that the equals method takes an Object in its parentheses (Don't get confused by the anObject bit! That is just an example name.) A bit like how we create a string like:

String aString = "I'm a string!";

aString is simply the name and String is the type.

The object is almost "every thing with a value" so, things like Strings and Integers are objects but, something like an if statement is not. The best way to know what's an object is to look at the documentation for a String:

Documentation ref image

Here, it shows that this "String" comes from the "Object" class and therefore, it is an object! Most things you'll come across are in fact objects, including arrays (A group of multiple objects).


Now, the description of the method. Us programmers use this just to understand what the method actually does! It can be overwhelming the amount of information there is about the method but, basically, it is saying that it compares a string to an object (A string is another object remember and we're comparing a string to a string). If the two values are the same, it will return true, if they are different, it will return false.


All of what applies here applies to any other method (Obviously, the method will be different and they will act differently). I hope this helps and, if you do have any other questions, feel free to ask :)