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 Strings, Variables, and Formatting

Melina Ortiz
Melina Ortiz
958 Points

I dont know what im doing wrong

I can't see what happen the code is not working

Name.java
// I have setup a java.io.Console object for you named console
String firstName = "Melina";
console.printf (" %S can code in java, firstName");

3 Answers

Try putting the ", firstName" part of the string outside and next to the string. Code challenges are also very picky, so if you include a space in the end of a string then the challenge won't let you pass.

A couple errors:

  • You have an extra space in the beginning of your string.
  • Try changing the "%S" (Java is super picky about cases!) to "%s".
  • You said "java" try changing it to "Java" (I think you know why)

Take a look at these:

This is your code

// I have setup a java.io.Console object for you named console
String firstName = "Melina";
console.printf (" %S can code in java, firstName");

This is the code that actually would work:

// I have setup a java.io.Console object for you named console
String firstName = "Melina";
console.printf("%s can code in Java", firstName);

Good luck and I hope this helps! ~Alex

EDITED

Brendon Butler
Brendon Butler
4,254 Points

The printf() function takes multiple parameters. Each parameter should be separated by a comma. In the snippit you posted, your comma is within the quotations. (Quotations define a String in Java).

console.printf ("%S can code in java, firstName");
// should be this
console.printf("%s can code in java", firstName);

Nice one, Brendon! However there's a couple more mistakes. Take a look here:

  • "java" should be "Java"
  • There's an extra space in the beginning of her string.

These are required because code challenges are especially picky and also Java is picky.

Good luck

EDITED

Brendon Butler
Brendon Butler
4,254 Points

... I just copied her code

Also, it shouldnt be Console.. It should be console.

Oh, whatever lol I'm new to Java and I'm too used to C#.

Thanks for telling me!

Brendon Butler
Brendon Butler
4,254 Points

Understandable. I dont know C#, but in java, static class variables and methods can be referenced using the capital class name.

public class Hello {
  public static void world() {
    // do stuff
  }

  publid static void world2() {
    // do stuff
  }
}

public class Other {
  public void otherMethod() {
    // you can access the world method either way
    Hello.world();

    Hello hello = new Hello();
    hello.world();

    // the only way to access the world2 is from a variable of the Hello type
    hello.world2();
  }
}

In the challenge, they say this "I have setup a java.io.Console object for you named console" so you would use the variable console to access its methods.

Cool.

(lol inside of me im all confused...)

Melina Ortiz
Melina Ortiz
958 Points

Thanks so much! I see I have tons of questions!

Your pleasure :)