1

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.

1
  • Perhaps you could use float for the magnitude? Commented Jan 26, 2014 at 6:08

2 Answers 2

4

You're mixing the ways to access the items in a List. Using the enhanced for loop like you have eliminates the counter variable that you'd have if you counted manually. Instead, you use the quake variable, which gets set to each value sequentially:

Earthquake largest = quakes.get(0); // start with whatever's first
for(Earthquake quake: earthquakes) {
    if(quake.getMagnitude() > largest.getMagnitude()) {
        largest = quake;
    }
}

There are a few other issues as well:

  • Use just List instead of ArrayList for method parameters. This lets the person using your code send any kind of List over.
  • You can't compare objects using < and >. You can, however, implement the Comparable interface and provide a compareTo() method that will tell you which earthquake has a larger magnitude. Then you can just use Collections.max(earthquakes).
  • I don't know what you were trying to do in your loop logic, but you can't just return the first thing you see. If you need to perform a sequential search, you have a variable for "largest value so far" or "smallest value so far" and look at each item, updating the variable if you find a new extreme.
Sign up to request clarification or add additional context in comments.

2 Comments

Ah Okay that makes more sense. What I was trying to do was get the magnitude of each earthquake and if as the loop porgressed it encountered one larger than the value stored it would replace that value with the new magnitude (as largestMag) then return the largest magnitude after all the earthquakes had been looked at
@user3236854 Your code looks like it may have been confused with some recursive search, which can work for binary, but for linear just walk through the items in turn.
0

the for loop you have used internally uses an iterator, it doesn't have a counter variable i. Instead of using earthquake.get(i), use quake

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.