Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java

Censoring Words - Using String Equality

In Java Basics under the exercise titled "Censoring Words - Using String Equality", Craig uses { } marks while typing out a command in the exercise. Why were the { } marks used in the spots that they were placed in the example below? Thank you.

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

2 Answers

He uses the {} because that is the proper syntax for the IF statement he used. It kind of looks confusing when it is all on one line like that. However, this is the proper method of using an IF statement. Look below at how I spaced out the wording and it should make more sense. Hope this helps.

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

if(insert argument){//if argument is true go into statements statements statements }//finished

Hi Brandon DeJesus, So the { goes after the if statement and argument that's inserted and then } after the final statement?

Fahad Mutair
Fahad Mutair
10,359 Points

hi Lee Preslan , in if statement with more than 1 statement you have to use {} in it's body like this

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

but with 1 statement inside if body can be like this , in this case you don't really need those {}

if (a==b) System.out.print("a equals b");