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 Harnessing the Power of Objects Constants

Ben Kaufman
Ben Kaufman
6,813 Points

Can't seem to create a new instance of the PezDispenser Object using jshell and I can't spot why

Ok so The best thing I can do I link each section of my code

Example.java

public class Example {

  public static void main(String[] args) {
    // Your amazing code goes here...

    PezDispenser dispenser = new PezDispenser("Qui Jon");

    System.out.printf("the dispenser is a %s \n",
                      dispenser.getCharacterName());

  String before = dispenser.swapHead("Darth Vader\n");
   System.out.printf("The swapped out head is %s the new head is %s\n", 
                     before,
                     dispenser.getCharacterName());
  }

}

PezDispenser.java

class PezDispenser {
  private String characterName;
  public final int MAX_PEZ = 12;
  public String getCharacterName(){
    return this.characterName;
  }


  public PezDispenser( String name) {

 this.characterName = name;

  }

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

Then I compiled it

javac PezDispenser.java

Then open jshell

then I entered the following

jshell> PezDispenser pd = new  PezDispenser("Yoda");                                                                                                
|  Error:                                                                                                                                           
|  cannot find symbol                                                                                                                               
|    symbol:   class PezDispenser                                                                                                                   
|  PezDispenser pd = new  PezDispenser("Yoda");                                                                                                     
|  ^----------^                                                                                                                                     
|  Error:                                                                                                                                           
|  cannot find symbol                                                                                                                               
|    symbol:   class PezDispenser                                                                                                                   
|  PezDispenser pd = new  PezDispenser("Yoda");                                                                                                     
|                         ^----------^             

I don't know why this isn't working and It is preventing me from progressing. I understand the concepts and what each piece is supposed to do, something is wonky though

3 Answers

jshell does not know anything about the fact that you previously compiled the classes. As a result, when you attempt to create a PezDispenser, it gives you an error. Jshell hasn't heard of a PezDispenser.

If you want to be able to create a PezDispenser in jshell, you first need to tell jshell about it. The easiest way would be to use the mouse to select and copy the contents of the file, then paste them into jshell.

Tim Barbarino
Tim Barbarino
5,449 Points

Adrian is right in saying that jshell doesn't know anything about any programs that you ran previously.

I believe the issue is that you did not open the file in jshell.

You can do this with the /open command.

Ie, /open PezDispenser.java

Then, you should encounter no problems with what you were trying.

Hi all,

I don't have an answer, but I am experiencing something similar to the above. In jshell I have put in the command /open PezDispenser.java and it seems to open with no errors, then when I try to input PezDispenser pd = new PezDispenser("Yoda"); I receive the same error as listed above?

I also noticed that Craig enters another line: /open PezDispenser.PezDispenser.class PezDispenser.java What does this accomplish? I attempted to input this to try something different but I receive: File 'PezDispenser.PezDispenser.class PezDispenser.java' for '/open' is not found.

I did read the comment about simply copying and pasting the code into jshell, but Craig doesn't show this process so I am confused as to why this would be suggested?

Any clarity would be appreciated.