• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

compiling error--identifer expected

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
numChild and numParents are intance variables. I have declared them at the begining of the codes.

Thanks
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Ernest and Rusty. Problem solved. You guys are awsome
 
reply
    Bookmark Topic Watch Topic
  • New Topic