Java Java Objects Meet Objects Final

saiful adl
saiful adl
560 Points

Illegal Start of type for System.out.printf

From what I can see it is the same as Craig's but it keeps throwing an error:

Example.java:8: error: no suitable method found for println(String,String)
System.out.println("The dispenser is %s %n",
^
method PrintStream.println() is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(boolean) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(int) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(long) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(float) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(double) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(char[]) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(String) is not applicable
(actual and formal argument lists differ in length)
method PrintStream.println(Object) is not applicable
(actual and formal argument lists differ in length)
1 error

My code:

public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...
    System.out.println("We are making a new Pez dispenser");
    PezDispenser dispenser = new PezDispenser("Yoda");

    System.out.println("The dispenser is %s %n",
                      dispenser.getCharacterName()
                     );

    String before = dispenser.swapHead("Darth Vader");
    System.out.printf("It was %s but Chris switched it to %s %n",
                      before,
                      dispenser.getCharacterName());
     }

  }

1 Answer

Daniel Turato
seal-mask
PLUS
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,118 Points

These two lines need to be inside the main method. Not sure if this will fix it but have a try.

String before = dispenser.swapHead("Darth Vader"); 
System.out.printf("hello");
saiful adl
saiful adl
560 Points

I have updated the code and error.

I tried to start the video from beginning and try again but still doesn't work. For some reason System.out.printf is the one that keeps throwing an error. Do I need to import a package for this to work?

It should take a String but it's saying that's not valid. I don't get it.

Daniel Turato
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,118 Points

You need to change this as well, as you're trying to use a formatter on println instead of printf

 System.out.println("The dispenser is %s %n",
                      dispenser.getCharacterName()
                     );

to this:

 System.out.printf("The dispenser is %s %n",
                      dispenser.getCharacterName()
                     );
saiful adl
saiful adl
560 Points

Thanks! That seems to have fixed the problem. I thought both are valid forms of code to print something to the console?

Daniel Turato
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Daniel Turato
Java Web Development Techdegree Graduate 30,118 Points

You're right that they're both valid options to print something to the console but if you want to use formatters such as %s ect then you need to use printf.