Basically I've been having difficulty using a for loop to find the largest magnitude of an earthquake stored in an arraylist in class observatory. I get an error telling me that the variable "i" in my for loop cannot be found for the method to find the largest magnitude recorded by the observatory.
class Observatory
{
private String name;
private String country;
private int yearStarted;
private int area;
private int runningTotal;
private ArrayList<Earthquake> earthquakes;
private ArrayList<String> observatories;
Scanner userInput = newScanner(System.in);
private double largestMag(ArrayList<Earthquake> earthquake, double magnitude)
{
if (earthquake == null || earthquake.isEmpty()){
return 0;
}
for (Earthquake quake : earthquake) {
if (earthquake.get(0) < earthquake.get(i)){
return earthquake.get(i);
}
if (earthquake.get(0) > earthquake.get(i)){
return earthquake.get(0);
}
}
}
I know its something to do with the fact that in my earthquake class you add the earthquakes the array list is storing more than one piece of information?
this is my earthquake class
public class Earthquake
{
// instance variables - replace the example below with your own
private double magnitude;
private double latitude;
private double longitude;
private int yearOfEvent;
private String position;
private String details;
public Earthquake()
{
magnitude = 0;
latitude = 0;
longitude = 0;
yearOfEvent = 0;
}
public void setMagnitude(double magnitude)
{
this.magnitude= magnitude;
}
public double getMagnitude()
{
return magnitude;
}
public void setYear(int yearOfEvent)
{
this.yearOfEvent = yearOfEvent;
}
public int getYear()
{
return yearOfEvent;
}
public void setPosition(double latitude, double longitude)
{
this.latitude = latitude;
this.longitude = longitude;
}
public String getPosition()
{
position = latitude + ", " + longitude;
return position;
}
public String getDetails()
{
details = magnitude + " ," + latitude + "," + longitude + ","+ yearOfEvent;
return details;
}
}
I'm not sure how to get round the problem of pulling out the magnitude from the array list to use for calculations in my method. thanks.
floatfor themagnitude?