0

I have a class called Runner which looks like this:

public class Runner 
{
   private static int nextNumber= 1;

   private int number;       
   private String name;      
   private String ageGroup;  
   private int time;         


   public Runner()
   {
      super();
      this.name = "";
      this.ageGroup = "standard";
      this.time = 0;
      this.number = nextNumber++;
    }

It also includes standard getter and setter methods. In another class I am trying to write a method that reads in data from a text file and assigns the data to a list. My method currently looks like this:

   public void readInRunners()
   {
      String pathname = OUFileChooser.getFilename();
      File aFile = new File(pathname);
      Scanner bufferedScanner = null;
      List<Runner> runners = new ArrayList<>();

      try
      {
         String name;
         int age;
         String ageGroup;
         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();

            if(age < 18)
            {
               ageGroup = ("junior");
            }

            else if(age >= 55)
            {
               ageGroup = ("senior");
            }

            else if(age >= 18 && age < 55)
            {
               ageGroup = ("standard");
            }

            runners.add(new Runner(name, ageGroup));
         }
      }

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

      finally
      {
         try
         {
            bufferedScanner.close();
         }
         catch (Exception anException)
         {
            System.out.println("Error: " + anException);
         }
      }

I am getting an error on this line:

runners.add(new Runner(name, ageGroup));

I know that is isn't compiling because of the incompatible argument but I'm unsure how to phrase this otherwise.

Any advice would be greatly appreciated.

aTubOfAdam
  • 91
  • 1
  • 7

3 Answers3

0

I think that you should add a constructor

public Runner(String name, String age)
   {
     this.name = name,
     this.age= age;
   }

you have only empty constructor

public Runner(){// some code}
Hamdy
  • 430
  • 1
  • 6
  • 19
0

You need to have a parameterised constructor like in your runner class

public Runner(String name,String ageGroup)
   {
      this.name = name;
      this.ageGroup = ageGroup;
      this.time = 0;
      this.number = nextNumber++;
    }
amudhan3093
  • 740
  • 9
  • 17
  • Is there any reason why I might get the error: "variable ageGroup might not have been initialized" after doing this? – aTubOfAdam Mar 27 '17 at 16:14
  • Inside the try block you need to initialize like String ageGroup=""; since all the assignment statements are within if/else blocks – amudhan3093 Mar 27 '17 at 16:17
  • Try printing the age after you read from file.You will know the mistake – amudhan3093 Mar 27 '17 at 16:40
  • When I print out the ArrayList it's returning me the variable locations instead of the actual data. I've had this problem before but I don't remember the fix. – aTubOfAdam Mar 28 '17 at 12:29
  • You need to override toString() method in Runner class. http://stackoverflow.com/questions/29140402/how-do-i-print-my-java-object-without-getting-sometype2f92e0f4 – amudhan3093 Mar 28 '17 at 12:35
  • That's the one. Still getting issues, but don't think I want to labor the point any more in this thread. Thanks for the help. – aTubOfAdam Mar 28 '17 at 12:51
0

Just add those arguments to the constructor in the runner class:

  public Runner(String name, String ageGroup)
   {
      super();
      this.name = name;
      this.ageGroup = ageGroup;
      this.time = 0;
      this.number = nextNumber++;
    }
matiastofteby
  • 431
  • 4
  • 18