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 3 of 3

Thread: Exercise 149

  1. #1
    Member
    Join Date
    Aug 2013
    Posts
    101
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Exercise 149

    Hi, I'm having trouble with this exercise. Any help would be appreciated. I think I only have one part wrong, but I'm not sure. I think it has to do with checking for x in ArrayList a. I'm pretty sure I'm not checking for "x" correctly. The error I'm getting is "not a statement: for (x : a) {" or when I put "Person x : a" I get an error that says "modifier expected"

    Here is the exercise instructions and prewritten code:

     
    In the following definition of the Person class, complete the definition of the includeMeIn method. This takes as its only argument an ArrayList a that contains nothing but Person objects. It adds the instance to the ArrayList, provided only that there is not already a Person object in the ArrayList with the same name:
     
    public class Person
    {
      private String myName;
     
      public Person( String name )
      {
        myName = name;
      }
     
      public String getName()
      {
        return myName;
      }
     
     
    //my code would go here
     
    //here is what I have so far:
     
    public void includeMeIn( ArrayList<Person> a )
     
    {
     
        for (Person x : a){
     
            if (Person.new == x){
     
                a.remove(x);
     
            }
     
        }
     
    }
     
    }
     
    public class MainClass
    {
      public static void main( String[] args )
      {
     
    //and here is some other prewritten code that I can modify:
     
    ArrayList<Person> people = new ArrayList<Person>();
     
    Person p1 = new Person( "fred" );
     
    Person p2 = new Person( "eric" );
     
    Person p3 = new Person( "jason" );
     
    Person p4 = new Person( "davina" );    
     
    Person p5 = new Person( "eric" );
     
     
     
    p1.includeMeIn( people );
     
    p2.includeMeIn( people );
     
    p3.includeMeIn( people );
     
    p4.includeMeIn( people );
     
    p5.includeMeIn( people );
     
     
     
    for ( Person p : people )
     
      System.out.println( p.getName() );
     
     //That part was modifiable
     
      }
    }


  2. #2
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Exercise 149

    for (x : a) {" or when I put "Person x : a" I get an error that says "modifier expected"
    I believe that you have compilation error in this part of your code.
    That is not a valid syntax.
    if (Person.new == x)

    May I know what are you trying to check with that statement?

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: Exercise 149

    From what I can see, the includeMeIn method is to check if the name stored within the Person object is already in the ArrayList a that is passed into this method.

    Take a look at the ArrayList API doc at ArrayList (Java Platform SE 7 ). This class contains a method that can help you do the checking.

Similar Threads

  1. accessor and modifier method exercise; exercise 100
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 30th, 2013, 09:18 PM
  2. Exercise 95 (I KNOW, I'm at an earlier exercise)
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 9
    Last Post: December 24th, 2013, 07:42 PM
  3. Exercise 86
    By ghostheadx in forum What's Wrong With My Code?
    Replies: 11
    Last Post: December 7th, 2013, 10:31 PM
  4. Exercise
    By keepStriving in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 21st, 2013, 06:58 PM
  5. Help with exercise.
    By javapol in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 8th, 2013, 08:40 PM

Tags for this Thread