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 Getting Started with Java Receiving Input

Whats wrong!

My code looks right to the video standards, but this is what I get!

(Now my computer won let me paste. it says its not right, and that I have no thing that will bring out the code.)

Here is my code:

import java.io.Console;

public class Introductions {

public static void main(String[] args) {
    Console console = System.console();
    // Welcome to the Introductions program!  Your code goes below here
    String firstName = console.readLine ("What is YOUR name!  ");
  console.printf("Hey! Im %s and I'm a Cat FANATIC!/n",firstName);
  console.printf("I also think I, %s, love this!/n", firstName);

} }

3 Answers

Instead of /n, replace it with \n and your code should work properly. I hope this helps!

My \ looks like a Chinese character in the code. I dont understand. I already tried to replace it.

Hi Breck!

When taking input from a command line format and attempting to output a formatted version, it is important to be careful about your syntax. One miskey could cause a minor to severe bug in your program and bring forth unwanted issues!

Here my my code, try copying and pasting it, because everything seems to be correct besides the use of '/' slash, instead of back-slash.

import java.io.Console;

public class Introductions {
    public static void main(String[] args) {
        Console console = System.console();
        // Welcome to the Introductions program!  Your code goes below here
        String firstName = "Christopher";
        String myFriend = console.readLine("What is your name?  ");
        console.printf("Hello, my name is %s\n", firstName);
        console.printf("My friend, %s, is learning how to write Java\n", myFriend);  
    }
}

Another thing I want to suggest is that you read the Java Styleguide which can be found at the following link: https://google.github.io/styleguide/javaguide.html

Sure style may vary depending on the corporation that you work with, but nonetheless, you will find developing a good habit of structuring your code well early on to be highly rewarding in the long-term future.

Below is an example of some code that I generated that is also quite good, which is not necessarily required for such a simple function, because it is also agreed upon that great code should be read by all.

class Main {

    public static float area(float base, float height) {
    /**
    *
    * Return the area of a triangle with dimensions base
    * and height.
    *
    * @param base           base of a triangle
    * @param height        height of triangle
    * @return
    *
    */

    return base * height / 2;
    }
}

If you have any further questions please feel free to get in touch with me, I would be happy to help you.

You can also Tweet at me directly via www.twitter.com/DoctaCloak

If \n isn't working on your computer for some reasons, you can also use %n. In fact, one should prefer %n over \n since it is platform independent and works everywhere. Good luck!