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 trialNoelle Kuzov
Courses Plus Student 394 PointsTwo error codes on creating class
I keep getting two error codes on the console for Example.java the code is exactly the same, but it won't let me run it. This is my code:
System.out.println("We are a making a new Pez Dispenser.");
PezDispenser dispenser = new PezDispenser() ;
System.out.printf("The dispenser character is %s\n" dispenser .mCharacterName);
These are the errors I get when I try to run the above code:
Example.java:7: error: illegal start of expression
System.out.printf("The dispenser character is %s\n" dispenser.m CharacterName);
^
Example.java:7: error: ';' expected
System.out.printf("The dispenser character is %s\n" dispenser.mCharacterName);
^
8 Answers
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHello, this is actually a really simple fix where something small was forgotten, be sure to add a "," after you're formatted sentence, so it should look something below.
System.out.printf("The dispenser character is %s\n", dispenser.mCharacterName);
Noelle Kuzov
Courses Plus Student 394 PointsI tried it with the comma and without. either way, when I run the code in the workspace, it keeps telling me that the period between dispenser and mCharacterName is not supposed to be there, or that it can't find it.
Ken Alger
Treehouse TeacherNoelle;
When you create your new PezDispenser
object, are you passing in a value? I believe the code in the video has:
PezDispenser dispenser = new PezDispenser("Yoda");
Unless you overloaded the PezDispenser()
method to give it a default value, that could be the source of your error messages.
Ken
Noelle Kuzov
Courses Plus Student 394 PointsUnfortunately, that didn't work either in fact the workspace created another error that says there is something wrong with the n in new as well as the error about the period in between dispenser and mCharacterName
Noelle Kuzov
Courses Plus Student 394 PointsThe code is still not working I have rewritten it multiple times using these suggestions, but the console still refuses to run the code.
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHello, here's anothing thing to try. I think the trouble might have been that you were calling a private variable in example .java, mCharacterName is the a class variable, let's try using the getter method that we made called getCharacterName(), it should look like below.
System.out.printf("The dispersenser character is %s\n", dispenser.getCharacterName());
Let me know if that does any better for the line in question.
Noelle Kuzov
Courses Plus Student 394 PointsUnfortunately, no. The workspace is now giving an error that says: (reached end of file while parsing) and the little arrow points to the space right after the bracket to close the code
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsThis is normal in programming, it means that your code is missing a closing bracket. Which is good news, you have cleared up one error and are now onto the next. Try adding another closing bracket at the end of your file, it might also be worthwhile to copy your whole example.java file as well.
However the good news is that error is done and we're onto the next, adding a closing bracket might clear that one up.
Noelle Kuzov
Courses Plus Student 394 PointsThe bracket is fixed now, but it still doesn't matter whether I use getCharacterName, or mCharacterName the console won't recognize the period between dispenser and whichever one I use.
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsHello sorry to hear that. Can you copy everything you have in your example.java and pezdispeser.java?
I'll have to give it a look. Thanks
Ken Alger
Treehouse TeacherNoelle;
I would echo what Rob said about posting your code and also what your current error message(s) is/are. If you are continuing to get Illegal Start of Expression errors you probably are missing a closing bracket elsewhere in your code that is defining a method inside another method. That doesn't make Java happy. Another option for the error might be designating a method variable public when it should be a local variable, that sort of thing.
Post your code and we'll have a look.
Happy coding,
Ken
Noelle Kuzov
Courses Plus Student 394 Pointsokay here is the code I'm working with for the PezDispenser. java file
public class PezDispenser {
public String mcharacterName = "Yoda";
}
and for the Example. java file
public class Example {
public static void main(String[] args) {
// Your amazing code goes here...
System.out.println("We are a making a new Pez Dispenser.");
PezDispenser dispenser = new PezDispenser() ;
System.out.printf("The dispersenser character is %s\n",
dispenser.mCharacterName);
}
} ```
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsIn your PezDispenser.java try changing the line to
mCharacterName instead of mcharacterName;
I think this might have been just a typo.
It should look something like
public class PezDispenser {
public String mCharacterName = "Yoda";
}
Let me know what it says after that.
Thanks.
Noelle Kuzov
Courses Plus Student 394 PointsFunny part is, that the C was uppercase in the first place, I'm not sure how it became lower case, but at least it works now thanks.
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 PointsGlad to hear it!
Let me know if there's any more troubles
Ken Alger
Treehouse TeacherKen Alger
Treehouse TeacherEdited for markup
Please see this post for instructions on posting code to the forum.