Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Not sure where/what my error is.

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Not sure where/what my error is.

    I have two classes: Person and PersonDemo.
    Person holds all the methods, etc, etc, whilst PersonDemo is as named, just for a demo.
    However, PersonDemo throws up a lot of errors in my Eclipse console, but won't

    public class Person {
     
    	String forename;
    	String surname;
    	int age;
     
    	public String getName(Person nameToGet) {
    		return forename + surname;
    	}
     
    	public Person(Person ID, String createdForename, String createdSurname, int createdAge) {
    		this.forename = createdForename;
    		this.surname = createdSurname;
    		this.age = createdAge;
    	}
     
    	public void printPersonsInfo(Person infoToGet) {
    		System.out.println("Name = "+getName(infoToGet)+"; Age = " + infoToGet.age);
    	}
     
    }
    ^No errors in that class.

    public class PersonDemo {
    	Person(johnSmith001, "John", "Smith", 43);//Error here, on "Person("
     
    	public static void main(String args[]) {
    		printPersonsInfo(johnSmith001);//Error here, on "johnSmith001"
    	}
     
    }
    ^I get errors on lines 2 and 5.

    Upon running the demo, "Name = John Smith; Age = 43" should be printed into the console.


  2. #2
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Not sure where/what my error is.

    Move the first line inside of main. Then at the beginning of that line, declare a Person variable, and add the new keyword (because that is how you create new objects when calling constructors)

    Person myPerson = new (what you already have goes here).

    Then when you call printPersonsInfo, you need to use your reference to the Person object you just created

    myPerson.printPersonsInfo(...)

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Not sure where/what my error is.

    Person(johnSmith001, "John", "Smith", 43);//Error here, on "Person("

    Person p = new Person(Person x, "John", "Smith", 43);

    Ok, you don't have a variable called ID.

  4. #4
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where/what my error is.

    public class Person {
    	String forename;
    	String surname;
    	int age;
     
    	public String getName(Person nameToGet) {
    		return forename + " " + surname;
    	}
     
    	public Person(String createdForename, String createdSurname, int createdAge) {
    		this.forename = createdForename;
    		this.surname = createdSurname;
    		this.age = createdAge;
    	}
     
    	public void printPersonsInfo() {
    		System.out.println("Name = "+getName(this)+"; Age = " + this.age);
    	}
    }
    public class PersonDemo {
    	public static void main(String args[]) {
    		Person johnSmith001 = new Person("John", "Smith", 43);
    		johnSmith001.printPersonsInfo();
    	}
    }

    Now if I was to create a random Person object (via the program) how would I do it? (I wanted the object's name to derive from their first name and last name.

  5. #5
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Not sure where/what my error is.

    Your methods don't need to take in a Person object as an argument...... They already have a reference to the person in the "this" variable (or you can just call getName(), and directly access the age variable

    What exactly do you mean by a random person?
    Last edited by DavidFongs; November 17th, 2010 at 03:04 PM.

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Not sure where/what my error is.

    Let's see what this does:

     
    public class Person {
     
       private  String forename;
       private  String surname;
        private int  age;
        private String personID;
       private String name;
       public Person(String personID, String createdForename, String createdSurname, int createdAge) {
            this.forename = createdForename;
            this.surname = createdSurname;
            this.age = createdAge;
            this.personID = personID;
     
    setAge(createdAge);
    setPersonID(personID);
    setName(createdForename, createdSurname);
        }
     
    public void setPersonID(String personID2)
    {
    personID = personID2;
    }
     
    public String getPersonID()
    {
    return personID;
    }
     
    public void setForename(String forename2)
    {
    forename = forename2;
    }
     
    public String getForename()
    {
    return forename;
    }
     
    public void setSurname(String surname2)
    {
    surname = surname2;
    }
     
    public String getSurname()
    {
    return surname;
    }
     
    public void setAge (int age2)
    {
    age = age2;
    }
     
    public int getAge()
    {
    return age;
    }
     
    public void setName(String aForename, String aSurname)
    {
    name = aForename + " " + aSurname;
    setForename(aForename);
    setSurname(aSurname);
    }
     
    public void setName2()
    {
    name = getForeName() + " " + getSurname();
     
    }
     
     
        public String getName() {
            return name;
        }
     
     
     
        public void printPersonsInfo() {
            System.out.println("Name = "+getName()+ ";
      Age = " + infoToGet.age);
        }
     
    }

    public class PersonDemo {
     
     
        public static void main(String args[]) {
        Person p = new  Person("johnSmith001", "John", "Smith", 43);
       p.printPersonsInfo();
        }
     
    }

  7. #7
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where/what my error is.

    Thankyou Javapenguin! It worked, with a bit of tweaking!

  8. #8
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where/what my error is.

    Do you have any idea how I could list all of the people created so far?

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Not sure where/what my error is.

    The people's name or the Person themselves?

    There is a class called ArrayList<aClass> which works like an array in that it uses arrays internally but creates new arrays when it exceeds a certain size so that it can expand.

    Assuming you want the names, you could do this(remember to import java.util.*)

     
    ArrayList<String> aList = new ArrayList<String>();
     
    // for every person do this(remember index starts at 0)
     
    String str = p.getName();
     
    aList.add( index, str);
     
    // when you are done
     
    String str2 = aList.toString();
     
    System.out.println(str2);
     
    // that should get you a list, separated by commas, of all the names of the people you've added
    // but they won't be in alphabetical order unless you add them all that way
    Last edited by javapenguin; November 18th, 2010 at 12:46 PM.

  10. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Not sure where/what my error is.

    If you do want alphabetical order, and as long as you don't have duplicates, you can
    TreeSet<String> tSet = new TreeSet<String>();
     
    // for each person
    String str = p.getName();
    tSet.add(str);
     
    // when done
     
    String str2 = tSet.toString();
     
    System.out.println(str2);

    instead, but you cannot have duplicates.

  11. #11
    Junior Member
    Join Date
    Nov 2010
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Not sure where/what my error is.

    For the second one, where would I add that? When the person is created?
    Silly me, I see now
    Thanks, I'll test it now!
    Last edited by Jarba; November 19th, 2010 at 06:56 AM.