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 Objects: Got a couple of errors. Not sure what I'm doing wrong

These are the errors: System.out.println("FUN FACT: Thereare %d PEZ Dispenser %n",
^
method PrintStream.println() is not applicable (actual and formal argument lists differ in length)

Code is below: class PezDispenser{ public static final int MAX_PEZ = 12; final private String characterName; private int pezCount;

public PezDispenser (String characterName) { this.characterName = characterName; pezCount = 0; }

public void fill() { pezCount = MAX_PEZ; }

public String getCharacterName(){ return characterName; } public String swapHead(String characterName){ String originalCharacterName = this.characterName; this.characterName = characterName; return originalCharacterName; } }

3 Answers

Hi Kevin the problem here is you used println

 System.out.println("FUN FACT: Thereare %d PEZ Dispenser %n",

since you want to use %d to print out an integer you need to use printf():

 System.out.printf("FUN FACT: Thereare %d PEZ Dispenser %n",

and do not forget to put the int variable or constant you wish to insert in %d place..

The System.out.println() is designed to print out Strings and in the end of the Strings give a new line. Also it does not allow any escape characters such which begin by % or \ in some OS to be used here. Therefore your %d statement is considered an error by the Java compiler.

On the other hand System.out.printf() with printf method accepts formatted form of printing out it allows the developers to use % escape characters to be substituted by variables, constants or new line command. However, if you are using printf please remember that it will not give you new line in the end of the printed sentences for free. Instead you need to use %n (escape code for new line) as you have done yourself earlier.

I hope this will help you a little. I am sorry since I cannot see your full code this is the best I can do.

Hi Yanour

Thank you for your help. Your solution has helped the first problem. However. I have another error on line 20:

./PezDispenser.java:20: error: cannot assign a value to a final variable characterName this.characterName = characterName;

Hi Kevin

First of all here is the code that you wrote: (I add some comment to give you pointer what went wrong with your code when it was compiled). If you already understand the problem by reading the comments inside the code then it is great. But if you still curious on why this is invoking the errors there are some additional quite long and I am a bit worry it will be boring explanation below the code..

class PezDispenser{ 
public static final int MAX_PEZ = 12; 
final private String characterName; //this variable is already final so you cannot change the this.characterName variable
private int pezCount;

public PezDispenser (String characterName) { 
    this.characterName = characterName; 
    pezCount = 0; }

public void fill() {   
    pezCount = MAX_PEZ; 
    }

public String getCharacterName(){ 
    return characterName; 
    } 

public String swapHead(String characterName){ //you need to delete this whole method if you want to fix the error
    String originalCharacterName = this.characterName; 
    this.characterName = characterName; //you attempt to change the this.characterName variable value this will invoke errors
    return originalCharacterName; 
    }
 }

The main reason of your error is because you already put final attribute on the pivate String characterName variable declaration. This will cause the variable this.characterName (I use this to differentiate it with variable also called characterName from swapHead method) is locked with the first variable it received when you instantiate PezDispenser class object (or let us change this alien terms into 'make' new PezDisepenser) in Example.java file.

Do you wrote this in Example.java code?

 PezDispenser dispenser = new PezDispenser("Yoda");

If yes then the value of characterName in the dispenser object is already locked = "Yoda".

But in line 20 or something in PezDispenser.java code you have this switchHead method:

public String swapHead(String characterName){ 
    String originalCharacterName = this.characterName; 
    this.characterName = characterName; //this is the main source of the error
    return originalCharacterName; 
    }

please focus on the commented section of the main source of the error. In that section of the code the method swapHead try to alter the value of this.characterName with characterName input passed by the user to swapHead method. This is a violation of what you declared earlier that variable called this.characterName is already final. Thus it will get you an error.

If you delete the commented line in that code section I think you will fix the error. However, I recommend you delete the whole public String swapHead(String characterName) method. The reason is when you declare a variable as final it is to prevent anyone to alter it using any method. This reason also applied in this case where you want to prevent anyone using swapHead method changing your characterName variable value of "Yoda" to other name. Therefore, the method itself is already useless.and potentially will create bug just like in your case.

That is all from me. Sorry for the long explanation I do my best to keep it short. I hoe it will not make you bored. I hope this will help you a little.

PS: If you do not mind please click the Markdown Cheatsheet link below your text box if you want to post a questions, answers, and/or comments. I suggest you learn how to embed a code in the text box so it will be easier to read by other members of the community.

Thanks again for your help mate. I read you notes and also went back over the Treehouse tutorials. I can see that the method was used as an example of what happens when you try to make changes to a class that is final. I realise now that forgot to delete the code. Your explanation reinforced this idea. Greatly appreciated. makies a lot more sense now :o)