I have a revision problem that needs me to add a randomly generated double to be added to an existing arrayList entrants
, this happens in the method runRace
, and I am pretty sure I have coded this part of runRace
correctly, but in this method I am getting an incompatible types error when I try to pass the value of randomRaceTime
to the variable time in the Entrant class.
This the only variable that was given that hasn't been used yet, so am more than confident this is were it has to go and then this is added to the ArrayList as another entry.
But I am struggling to understand why I am getting an incompatible type error as I have declared all previous variables as doubles, or I am passing the data incorrectly
I have included both complete classes any help would be greatly appreciated as I thought I had grasped this one.
public class WCCR
{
// instance variables - replace the example below with your own
private ArrayList<String> entrants;
static Scanner myInput = new Scanner(System.in); // initialise user input
private double randomRaceTime;
/**
* Constructor for objects of class WCCR
*/
public WCCR()
{
// initialise array list of objects Entrants
entrants = new ArrayList<>();
}
public void readInEntrants()
{
System.out.println("Enter your file");
String textFileInput = myInput.nextLine(); // takes input from user
entrants = new ArrayList<>();
try
{
FileReader file = new FileReader(textFileInput);//textFileInput); // reads the file
BufferedReader buffer = new BufferedReader(file);
String fileLine = "";
while ((fileLine = buffer.readLine())!= null) // reads through the file
{
entrants.add(fileLine);
}
buffer.close();
}
catch (IOException e) // catches input/output error
{
System.out.println("A read error has occurred");
}
// System.out.println("The Entrants are : " + entrants); // used to test the file has been added to the array list
}
public double generateTime()
{
int size = entrants.size();
for(int y = 0; y < size; y++)
{
Random r = new Random();
randomRaceTime = 30.00 + (60.00 - 30.00) * r.nextDouble();
}
//System.out.println("The race times are: " + randomRaceTime);
}
public void runRace()
{
int size = entrants.size();
entrants = new ArrayList<>();
for(int y = 0; y < size; y++)
{
Entrant time = randomRaceTime; // variable time from Entrant Class assigned the value of randomRacetime
entrants.add(time); // time added to the array List entrants
System.out.println("The Entrants are with times are: " + entrants);
// System.out.println("arrayList size is :" + size); used to test array is correct
}
}
}
public class Entrant
{
/* instance variables */
private int number; // entrant's number
private String name; // entrant's name
private String category; //
private double time; // entrant's time in seconds
/**
* Constructor for objects of class Entrant.
*/
public Entrant()
{
super();
name = "";
category = "";
time = 0.0;
while(number > number + 1)
{
number++;
}
}
}
// getter & setter methods follow
The Entrants are with times are: [John Smith,E1, Mary Jones,E2, Jane Smith,E1, Meiling Wang,E3, Carlos Gonzalez,E3, Darren Edwards,E2, Melanie Roberts,E1, Mark Edwards,E2, Sham Singh,E3, Morag Stephens,E1, James Long,E2, David Clarke,E3, Robyn Armstrong,E3, Maxine Rogers,E2, Roisin Hughes,E1, Jonathan Miles,E1, Klaus Mueller,E2, Christine Miles,E1, Andrew Smith,E3, Astrid Witzany,E3, Nico van Dord,E2, Yves Thirionet,E1, Andy Simpson,E2, Brian Dwelly,E3, Vera Aland,E1, Barry Milsom,E2, Simon Harris,E3, Helen Jones,E3, Justin Lawson,E2, Sarah Jennings,E1]
is the whole array when called, but should add another value per entry of the randomly generated double
You decleared entrants as ArrayList<String>
but I think it should be ArrayList<Entrant>
instead.
You can't add Entrants to list of Strings.
Secondly you cant assingn a double
(randomRaceTime
) to a variable declared as Entrant