1
public class Person{

    public static void main(String[] args){
        //Class constructor
        Person(String name, String status, int Age){
            this.name = name;
            this.status = status;
            this.Age = Age;
        }
        //Object creation
        Person one = new Person("John", "Single", 18);
        Person two = new Person("Kez", "Single", 21);
        Person three = new Person("Bob", "Married", 31);

        //Print out attributes
        System.out.println("Person one Profile: %s/t%s/t%d", +one.name,    +one.status, +one.Age);
        System.out.println("Person two Profile: %s/t%s/t%d", +two.name, +two.status, +two.Age);
        System.out.println("Person three Profile: %s/t%s/t%d", +three.name, +three.status, +three.Age);
    }
}

What could be wrong with my code? The errors am being warned of by the compiler are not helping me understand the problem.

2
  • 1
    Quick note: even though this is an easy solution, t's typically more helpful to post the actual error message (s) along with your code so we can more directly address your problem. Commented Apr 22, 2015 at 22:55
  • 1
    Sure I've got that thanks. Commented Apr 22, 2015 at 23:10

3 Answers 3

1

There are 5 problems with your code. Firstly, variables should begin with lower case letters, so I have changed Age to age. Secondly, your constructor should not be in the main method. Thirdly, you want printf rather than println. Also you need to get rid of those + signs. + is an operator that requires 2 Strings (one on either side), not just one String. Finally, I assume you mean \t rather than /t. I have changed this and also added two line breaks.

The corrected code is

public class Person {

    private String name;
    private String status;
    private int age;

    Person(String name, String status, int age){
        this.name = name;
        this.status = status;
        this.age = age;
    }

    public static void main(String[] args) {
        //Object creation
        Person one = new Person("John", "Single", 18);
        Person two = new Person("Kez", "Single", 21);
        Person three = new Person("Bob", "Married", 31);

        //Print out attributes
        System.out.printf("Person one Profile: %s\t%s\t%d\n", one.name, one.status, one.age);
        System.out.printf("Person two Profile: %s\t%s\t%d\n", two.name, two.status, two.age);
        System.out.printf("Person three Profile: %s\t%s\t%d", three.name, three.status, three.age);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Josiah Glad I could help. Don't forget to accept or upvote any of the 3 answers you found helpful.
@pbabcdefp Notice that OP don't have enought reputation to upvote.
0

Constructor are a special kind of method. These generally need to be outside of other methods.

//Class constructor
Person(String name, String status, int Age){
    this.name = name;
    this.status = status;
    this.Age = Age;
}
public static void main(String[] args){

  //Object creation
  Person one = new Person("John", "Single", 18);
  Person two = new Person("Kez", "Single", 21);
  Person three = new Person("Bob", "Married", 31);

  //Print out attributes
  System.out.println("Person one Profile: %s/t%s/t%d", +one.name,    +one.status, +one.Age);
  System.out.println("Person two Profile: %s/t%s/t%d", +two.name, +two.status, +two.Age);
  System.out.println("Person three Profile: %s/t%s/t%d", +three.name, +three.status, +three.Age);
}

Constructors are in every class by default. if you didnt have a constructuro in your Person class then you would have a default one so you could call

new Person();

By adding a constructor with string, string, int then you loose this default constructor. If you want a default one with no input parameters then you will need to include it again

Person(){}

Comments

0

Try this:

class Test{
  public static void main(String[] args){

    //Object creation
    Person one = new Person("John", "Single", 18);
    Person two = new Person("Kez", "Single", 21);
    Person three = new Person("Bob", "Married", 31);

    //Print out attributes
    System.out.printf("Person one Profile:\t %s\t%s\t%d%n", one.name, one.status, one.age);
    System.out.printf("Person two Profile:\t %s\t%s\t%d%n", two.name, two.status, two.age);
    System.out.printf("Person three Profile: %s\t%s\t%d%n", three.name, three.status, three.age);
  }
}

//Class constructor
class Person{
  //object fields
  String name;
  String status;
  int age;

  //constructor
  Person(String name, String status, int age){
    //overloading... if you use different names you can remove 'this'
    this.name = name;
    this.status = status;
    this.age = age;
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.