Results 1 to 4 of 4
  1. #1
    decon101 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Help With cannot find symbol method Error

    Hi This is my first post so im sorry if its not in the correct format. Ive been trying to get the following code working. I keep getting an error.. cannot find symbol method add(competitor) when i try to compile. with line 62 competitors.add(runner); highligthed. I have tried changing List<String> competitors = new ArrayList<String>(); to List<Competitor> competitors = new ArrayList<Competitor>(); but this does not work and ive got myself confused! Can any one help?


    Java Code:
    public class RaceAdmin
    {
       /* instance variables */
         private String competitors; // replace this example variable with your own
     
     
       /**
        * Default constructor for objects of class RaceAdmin
        */
       public RaceAdmin()
       {
          super();
          List<String> competitors = new ArrayList<String>();
       }
     
     
       /* instance methods */
     
       /**
        * A public instance method that take no argument
        * and returns no value.  Method reads from a file called
        * competitors.txt which contains pertinent information
        */
    public void readInCompetitors()
       {
          String pathname = OUFileChooser.getFilename();
          File compFile = new File(pathname);
          BufferedReader bufferedFileReader = null;
          try
          {
             bufferedFileReader = new BufferedReader(new FileReader(compFile));
             String currentLine = bufferedFileReader.readLine();
             String name;
             String sex;
             Integer age;
             String ageGroup;
             Scanner lineScanner;
             Integer time;
             while (currentLine != null)
             {
                lineScanner = new Scanner(currentLine);
                lineScanner.useDelimiter(",");
                Competitor runner = new Competitor();
                runner.setName(lineScanner.next());
                runner.setSex(lineScanner.next());
                System.out.print("e");
                age = lineScanner.nextInt();
                if (age < 18)
                {
                   runner.setAgeGroup("junior");
                }
                else if (age > 55)
                {
                   runner.setAgeGroup("senior");
                }
                else
                {
                   runner.setAgeGroup("standard");
                }
                runner.setTime(Integer.parseInt(OUDialog.request(" Enter Time for Competitor: ")));
                currentLine = bufferedFileReader.readLine();
                competitors.add(runner);
             }
          }
          catch (Exception anException)
          {
             System.out.println("An error has occur: " + anException);
          }
          finally
          {
             try
             {
                bufferedFileReader.close();
             }
             catch (Exception anException)
             {
                System.out.println("An error has occur: " + anException);
             }
          }
       }
    }

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: Help With cannot find symbol method Error

    Quote Originally Posted by decon101 View Post
    I have tried changing List<String> competitors = new ArrayList<String>(); to List<Competitor> competitors = new ArrayList<Competitor>(); but this does not work and ive got myself confused! Can any one help?
    That should work, however, please note the line 4! competitors is declared as String!! Your list is only visible in your constructor, so the competitors (String) covers your list! Delete line 4 and replace it with the creation of the list!

  3. #3
    decon101 is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help With cannot find symbol method Error

    Thank you!

    Thats seems to have worked, i cant believe it had me stuck for so long! I've amended my code to the following.. but i get an error uses unchecked or unsafe operation, how do I recompile wint -xlint:unchecked for details?

    Java Code:
    public class RaceAdmin
    {
       /* instance variables */
         private ArrayList competitors; // replace this example variable with your own
      
      
       /**
        * Default constructor for objects of class RaceAdmin
        */
       public RaceAdmin()
       {
          super();
          List<Competitor> competitors = new ArrayList<Competitor>();
       }
      
      
       /* instance methods */
      
       /**
        * A public instance method that take no argument
        * and returns no value.  Method reads from a file called
        * competitors.txt which contains pertinent information
        */
    public void readInCompetitors()
       {
          String pathname = OUFileChooser.getFilename();
          File compFile = new File(pathname);
          BufferedReader bufferedFileReader = null;
          try
          {
             bufferedFileReader = new BufferedReader(new FileReader(compFile));
             String currentLine = bufferedFileReader.readLine();
             String name;
             String sex;
             Integer age;
             String ageGroup;
             Scanner lineScanner;
             Integer time;
             while (currentLine != null)
             {
                lineScanner = new Scanner(currentLine);
                lineScanner.useDelimiter(",");
                Competitor runner = new Competitor();
                runner.setName(lineScanner.next());
                runner.setSex(lineScanner.next());
                System.out.print("e");
                age = lineScanner.nextInt();
                if (age < 18)
                {
                   runner.setAgeGroup("junior");
                }
                else if (age > 55)
                {
                   runner.setAgeGroup("senior");
                }
                else
                {
                   runner.setAgeGroup("standard");
                }
                runner.setTime(Integer.parseInt(OUDialog.request(" Enter Time for Competitor: ")));
                currentLine = bufferedFileReader.readLine();
                competitors.add(runner);
             }
          }
          catch (Exception anException)
          {
             System.out.println("An error has occur: " + anException);
          }
          finally
          {
             try
             {
                bufferedFileReader.close();
             }
             catch (Exception anException)
             {
                System.out.println("An error has occur: " + anException);
             }
          }
       }
    }
    Last edited by decon101; 05-01-2012 at 11:54 PM.

  4. #4
    soph is offline Member
    Join Date
    May 2010
    Posts
    9
    Rep Power
    0

    Default Re: Help With cannot find symbol method Error

    Line 4 needs to be;

    private ArrayList<Competitor> competitors;

    also the constructor code should be;

    competitors = new ArrayList<Competitor>();

Similar Threads

  1. Replies: 7
    Last Post: 10-25-2011, 05:47 AM
  2. Replies: 2
    Last Post: 03-08-2011, 04:08 PM
  3. HELP! can't find symbol-method
    By Jack9333 in forum New To Java
    Replies: 2
    Last Post: 03-04-2011, 02:48 AM
  4. Replies: 10
    Last Post: 12-22-2010, 02:12 AM
  5. Replies: 9
    Last Post: 10-18-2008, 09:26 PM

Posting Permissions

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