Results 1 to 7 of 7
  1. #1
    wildcardcrow is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Array list problems

    essentially my method does not like the stuff.add() (add method) even when i have written the add method:
    my array list is ArrayList<Runner> runners = new ArrayList<Runner>();

    /**
    * adds new runner to list
    */
    public boolean add(Runner Runner)
    {
    runners.add(new Runner());
    return true;
    }

    comes up with, cannot find symbol - method.add(Runner)

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Array list problems

    What symbol is not found? There are three in that statement.
    Please copy the full text of the error message and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Array list problems

    Hi,
    I am just wondering:
    The Runner argument is not used.
    Your method always returns true. So either you want to change that behaviour or simply change the return type to void.

    What type of variable is runners? If the error occurs there then you should check the implementation of that type if it really includes an add method.
    [Edit: sorry, you gave the definition of runners. Jumped to quickly to the source. Sorry for that. So that call seems to be valid.]
    Last edited by kneitzel; 04-28-2014 at 12:11 AM.

  4. #4
    wildcardcrow is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

    Default Re: Array list problems

    Quote Originally Posted by kneitzel View Post
    Hi,
    I am just wondering:
    The Runner argument is not used.
    Your method always returns true. So either you want to change that behaviour or simply change the return type to void.

    What type of variable is runners? If the error occurs there then you should check the implementation of that type if it really includes an add method.
    [Edit: sorry, you gave the definition of runners. Jumped to quickly to the source. Sorry for that. So that call seems to be valid.]
    guys sorry this is just a cut and paste for code,

    import java.util.*;
    import java.io.*;
    import ou.*;
    /**
    * Write a description of class MarathonAdmin here.
    *
    * @author (hoping like hell this works)
    * @version a million and six
    */
    public class MarathonAdmin
    {
    // instance variables - replace the example below with your own

    private Runner runners;
    /**
    * Constructor for objects of class MarathonAdmin
    */
    public MarathonAdmin()
    {
    // initialise instance variables
    ArrayList<Runner> runners = new ArrayList<Runner>();

    }
    /**
    * adds new runner to list
    */
    public boolean add(Runner Runner)
    {
    runners.add(new Runner());
    return true;

    }
    /**
    * reads runners from a file
    */
    public void readInRunners()
    {
    String pathname = OUFileChooser.getFilename();
    File aFile = new File(pathname);
    BufferedReader bufferedFileReader = null;
    Scanner bufferedScanner = null;

    try
    {
    int num = 0;
    String name;
    int age;
    int time = 0;
    String Group;
    Scanner lineScanner;
    String currentLine;
    bufferedScanner = new Scanner(new BufferedReader(new FileReader(aFile)));
    while(bufferedScanner.hasNextLine())
    {
    currentLine = bufferedScanner.nextLine();
    lineScanner = new Scanner(currentLine);
    lineScanner.useDelimiter(",");
    name = lineScanner.next();
    age = lineScanner.nextInt();
    runners = new Runner();

    runners.setName(name);
    runners.getNumber();
    runners.setTime(0);
    if(age <= 18)
    {
    Group = "Junior";
    }
    else if (age >= 55)
    {
    Group = "Senior";
    }
    else
    {
    Group = "Standard";
    }
    runners.setAgeGroup(Group);
    System.out.println();

    num++;



    }
    }
    catch(Exception anException)
    {
    System.out.println("ERROR: " + anException);
    }
    }






    }

    the add method was another attempt by me to make this work as runners.add(//name of runner) or any variation i have tried over the last week has failed. i dont know why this just wont make (15 runners that i have in a text file) i have tried while loops, a for loop and always with the same two ends; 1.one runner given 15 seperate attributes, or 2.one runner(the first on the list).
    the list is 15 names and ages, this is an assignment and i am severely stuck on this one thing.

    any help would be great guys
    thanks in advance

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Array list problems

    Quote Originally Posted by wildcardcrow View Post
    Java Code:
       public MarathonAdmin()
       {
          // initialise instance variables
         ArrayList<Runner> runners = new ArrayList<Runner>();
         
       }
        /**
        * adds new runner to list
        */
       public boolean add(Runner Runner)
       {
              runners.add(new Runner());
              return true;
              
       }
    That ArrayList 'runners' is a local variable in the constructor so it is not known in other methods; it is not an instance variable (as your comment claims); make it an instance variable instead.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    kneitzel is offline Senior Member
    Join Date
    Feb 2014
    Posts
    447
    Rep Power
    6

    Default Re: Array list problems

    And in addition to Jos reply:
    When runners is an ArrayList<Runenr>, the line
    runners = new Runner();
    is wrong, too.

    In there I would expect a Runner runner = new Runner(); and then set all properties.

    And you should change the Add method to something like
    Java Code:
    public boolean add(Runner runner)
    {
           return runners.add(runner);       
    }
    if you want to give other classes from outside your class the possibility to add runners.

    And then you can add the runner created to your runners ArrayList by claling Add(runner).

    With kind regards,

    Konrad

  7. #7
    wildcardcrow is offline Member
    Join Date
    Apr 2014
    Posts
    13
    Rep Power
    0

Similar Threads

  1. Array list problems :s
    By Manny123 in forum New To Java
    Replies: 9
    Last Post: 10-27-2012, 04:04 PM
  2. Problems inserting 1d array to 2d array grid
    By jumpgirl in forum New To Java
    Replies: 1
    Last Post: 03-26-2012, 04:04 AM
  3. Linked List, Array List time complexity
    By Rick99771977 in forum New To Java
    Replies: 4
    Last Post: 08-18-2011, 06:37 AM
  4. List problems
    By ZeCute in forum New To Java
    Replies: 0
    Last Post: 04-10-2011, 07:13 PM
  5. Replies: 2
    Last Post: 09-06-2010, 02:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •