• Post Reply Bookmark Topic Watch Topic
  • New Topic

compiling error--identifer expected  RSS feed

 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I am new to Java
When I compile my codes I have the following error
Could someone please help me out?
Thanks

error
<identifier> expected
public FamilyTree(int numPerson, numChild, numParents)


Here are my codes

import java.io.*;

public class FamilyTree implements Serializable
{
private static final long serialVersionUID = 7981277547209222959L;
private static final int DEFAULT_SIZE = 40;
Person[ ] familyRecord;
public int size;
private int numChild;
private int numParents;

//Constructors////////////////////////////////////////////////////////////////////////////////////////////
public FamilyTree()
{
familyRecord = new Person[DEFAULT_SIZE];
for (int i=0; i<familyRecord.size; i++)
familyRecord = new Person();
}

public FamilyTree(int numPersons)
{
familyRecord = new Person[numPersons];
for (int i=0; i><familyRecord.size; i++)
familyRecord = new Person();
}

public FamilyTree(int numPersons, numChild, numParents)
{
familyRecord = new Person[numPersons];
for (int i=0; i><familyRecord.size; i++)
familyRecord = new Person(numParents, numChild);
}


//Display the name, birthday, parent's names and children's names for each/////////////
//(primary) Person in a FamilyTree object///////////////////////////////////////////////////////////
public void display()
{
for (int i=0; i><familyRecord.size; i++)
{
if (familyRecord.getName() != null)
System.out.Println(familyRecord.getName());
}
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

When you write a constructor (or any method), you have to give the data type individually for each parameter, even if they're all the same type:

public FamilyTree(int numPerson, int numChild, int numParents)
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
numChild and numParents are intance variables. I have declared them at the begining of the codes.

Thanks
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


This is what is causing the problem as Ernest pointed out. The arguments of the constructor are local to the constructor, and nothing to do with any instance variables that might have the same name.

Do you realize that after the constructor execution completes, the values of the class level variables numParaents and numChild will be 0?
 
Jing Liang
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Ernest and Rusty. Problem solved. You guys are awsome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!