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 (Retired) Meet Objects Constructors

mahesh gurbaxani
mahesh gurbaxani
1,420 Points

please help with this code

When I compile ann.java (code shown below), I get the following error message: location: variable person1 of type person ann.java:11: error: cannot find symbol console.printf("the name is %s %s",person1.name, person1.wage); ^ symbol: variable wage location: variable person1 of type person 2 errors

public class person { public String mname; public int age;

public person(String name,int wage) // this is //the constructor { mname=name; age=wage; } }

import java.io.Console; public class ann { public static void main(String[]args) { Console console=System.console(); person person1=new person("bob",65); console.printf("my name is%s and age is %s", person1.name, person1.wage); } }

2 Answers

Hello,

At first glance it looks like the problem is with: console.printf("my name is%s and age is %s", person1.name, person1.wage); you have the string looking to replace two %s which are strings, instead you want it to read console.printf("my name is%s and age is %d", person1.name, person1.wage); where the second %s is %d which is the code to replace with an integer value.

In the future when posting code could you use the code markdown so that it is formatted in an easier to read manner?

Christopher Augg
Christopher Augg
21,223 Points

Hello Mahesh,

As James pointed out very well, you will need to correct the console.printf() with the proper %s for Strings and %d for numbers. However, there are some other problems I think we need to address as well. I went ahead and started a workspace and coded this all up with comments. Thanks for the practice assignment! Please read over the http://docs.oracle.com/javase/7/docs/api/java/io/Console.html documentation as it has a lot of good information about using Console.

The Person class:

public class Person { // A class should be capitalized

   public String mName;     //remember to use camelCase with the starting letter m (member variable) for clarity
   public int mAge;             //these should be made private and have getter methods

   public Person(String name, int age) { // Constructor should also be capitalized
    mName = name;
    mAge = age;

   }    
   }

The Ann class:

 import java.io.Console;

 public class Ann { //always capitalize the class name

    public static void main(String[] args) {

     Console console = null;  //initialize the console to null              
     Person person1 = new Person("bob", 65);

     try {
         console = System.console(); // can throw Exception so use try catch

         if (console != null) { // should check for null after attempting because 
                                                     // acquiring a console is system dependent
                                                     // The documentation talks about this.

                             // %s for strings, %d for numbers
             console.printf("my name is %s and age is %d", person1.mName, person1.mAge);
         }
     } catch (Exception ex) {

         ex.printStackTrace();
     }       
   }
}