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

Is there something wrong with my code?

Hi! I've finished java basics and I tried to code a litle after the course. So, coding on workspace i tried to improve the code learned during the course, but i simply cannot make it work! What is wrong with him? Or doesn't it work on workspace because of the import i've made?

import java.io.Console; import java.util.Arrays;

public class TreeStory {

public static void main(String[] args) {
    Console console = System.console();
    /*  Some terms:
        noun - Person, place or thing
        verb - An action
        adjective - A description used to modify or describe a noun
        Enter your amazing code here!
    */
  String ageRequest = console.readLine("How old are you?  ");
  int age = Integer.parseInt(ageRequest);
  if(age < 13){
    console.printf("Sorry, you're not allowed to enter");
    System.exit(0);
  }


  String[] namesProibited = {
    "fuck", "Rafael", "dork"
  };

  String name = console.readLine("Enter your name:  ");
  if(Arrays.asList(namesProibited).contains(name)){
    console.printf("Oh My God! You're so rude!");
    System.exit(0);
  }
    String adjective = console.readLine("Enter an adjective:  ");
    console.printf("%s is very %s", name, adjective);
}

}

1 Answer

Hi! :)

Well done on completing the Java Basics course! Keep it up! Your mistake is an easy one to make, don't worry about it. In the extra credit, they mention that the String has the method contains, but you're using it on an Array. Here is one solution of many, that will help to solve your problem:

String namesProhibited = "fuck Rafael dork";
String nameGiven = console.readLine("Enter a name:  ");

if(namesProhibited.contains(nameGiven)){
    console.printf("Oh My God! You're so rude! \n");
    System.exit(0);
} 

I have also included the whole class I wrote, with notes included on what you could try doing to improve the program if you want to try it, you may need to use google to find out how to do certain things I've suggested:

import java.io.Console;

public class TreeStory {

    public static void main(String[] args) {

        Console console = System.console();
        /*  Some terms:
            noun - Person, place or thing
            verb - An action
            adjective - A description used to modify or describe a noun
            Enter your amazing code here!
        */

        String ageGiven = console.readLine("Enter your age:  ");
        /*  Create a loop here that makes sure the program doesn't crash if the user doesn't enter a number.
            Something like: do (get do {get the age} while {age isn't int}
        */

        int age = Integer.parseInt(ageGiven);

        if (age < 13) {
            console.printf("Sorry, you're not allowed to enter \n");
            System.exit(0);
        } 

        String wordsProhibited = "fuck Rafael dork";
        String nameGiven = console.readLine("Enter a name:  ");
        /*  Move the adjective here and add the check for adjectives too
            Something like: if (contains nameGiven || contains adjective) {}
        */

        if (wordsProhibited.contains(nameGiven)) {
            console.printf("Oh My God! You're so rude! \n");
            System.exit(0);
        } 

        String adjective = console.readLine("Enter an adjective:  ");

        console.printf("%s, you're very %s. \n", nameGiven, adjective);
        System.exit(0);

        /*  Extra Credit:
            Put the program in a loop so that it doesn't have to exit if they put in a prohibited word. 
            Make sure you can't enter a prohibited word but with a different case, e.g. rafeal or Dork 
        */
    }
}

Just a quick tip on how to make sure your java code formats properly: :)

```java

String codeExample = "This is an example of java code";

```

It's always helpful to preview the message before you send it to make it easier for people to read your solution and help you. You should be able to copy and paste that section and paste your code inside. :)

reallllyyyyyy helpful! Thanks a lot!