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.
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}
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++;
}
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++;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.