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

ankit panwar
ankit panwar
436 Points

running perfectly on workspace, but when I tried it on challenge task it givs error for console

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 name= "ankit singh";
    console.printf("hello ankit \n" + name"\n");

} }

Name.java
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 name= "ankit singh";
        console.printf("hello ankit \n" + name"\n");
  }
}

2 Answers

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,073 Points

The interpolation allows you to make a "placeholder" inside of that string so you can put in that place what's inside of a variable, and with the concatenation you can only concatenate two strings. So if you want to concatenate a string with another thing you will have to make an explicit "parse".

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,073 Points

The code challenge is asking for you to make interpolation not concatenation, so instead of using the + operator use the % to interpolate a variable:

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

I hope that helps a little bit

ankit panwar
ankit panwar
436 Points

Thanks carlos bro , in testing window I only have to type the main code not main function and all that. What is the difference between %s and using + for concatenate