I have a very stupid problem that's giving me a major headache.
I defined a method which searches an ArrayList to find a zipcode:
public ZipCode findZip (int zip) {
ZipCode aZip = new ZipCode(0);
for(int i = 0; i < zips.size(); i++) {
if(zips.get(i).getZipCode() == zip)
aZip = zips.get(i);
else
aZip = null; }
return aZip; }
...But, I cannot call it for the life of me. It gives me the "cannot find symbol" error every time I call it, no matter what object I use or parameter I input.
The entire program so far (it can't be finished until I figure this out):import java.util.*;
import java.io.*;
import java.lang.Math;
public class ZipCodeDatabase {
//Field
private ArrayList<ZipCode> zips;
//Constructor
public ZipCodeDatabase () {
zips = new ArrayList<ZipCode> ();
}
//Mutator Method
public void readZipCodeData(String filename) {
Scanner inFS = null;
FileInputStream fileByteStream = null;
try{
// open the File and set delimiters
fileByteStream = new FileInputStream("zipcodes.txt");
inFS = new Scanner(fileByteStream);
inFS.useDelimiter("[,\r\n]+");
// continue while there is more data to read
while(inFS.hasNext()) {
//read in all input
int aZip = inFS.nextInt();
String aCity = inFS.next();
String aState = inFS.next();
double aLat = inFS.nextDouble();
double aLon = inFS.nextDouble();
//Create and add new zipcode
ZipCode newZip = new ZipCode(aZip, aCity, aState, aLat, aLon);
zips.add(newZip);
}
fileByteStream.close();
// Could not find file
}catch(FileNotFoundException error1) {
System.out.println("Failed to read the data file: " + filename);
// error while reading the file
}catch(IOException error2) {
System.out.println("Oops! Error related to: " + filename);
}
}
//Accessor Methods
public ZipCode findZip (int zip) {
ZipCode aZip = new ZipCode(0);
for(int i = 0; i < zips.size(); i++) {
if(zips.get(i).getZipCode() == zip)
aZip = zips.get(i);
else
aZip = null;
}
return aZip;
}
public int distance(int zip1, int zip2) {
int dist = 0;
double p1 = 0.0;
double p2 = 0.0;
double p3 = 0.0;
if(zips.findZip(zip1) == null || zips.findZip(zip2) == null)
dist = -1;
...
The error itself is:
cannot find symbol - Method findZip(int)
You are using a symbol here which has not been declared in any visible scope.
Using ZipCodeDatabase.findZip(int);
gives the following compiler error:
non-static method findZip(int) cannot be referenced from a static context
You are trying to reference an instance field or instance method from a static method.
I'm currently working through this, get back with more updates if needed. Thank you for all given help.
ZipCode itself isn't really in play for this issue, its just a bunch of set and get methods for the zips.
The problem comes from this line:
if(zips.findZip(zip1) == null || zips.findZip(zip2) == null)
Going up to the top of your class, we find the declaration of zips
, which is
private ArrayList<ZipCode> zips;
This is a problem, because your findZip
method is on the ZipCodeDatabase
class, not on ArrayList
. Since that call is happening from inside of a non-static method of ZipCodeDatabase
, you can simply remove the object you're calling it on.
if(findZip(zip1) == null || findZip(zip2) == null)
This is equivalent to using
if(this.findZip(zip1) == null || this.findZip(zip2) == null)
which calls the method on the current instance of the ZipCodeDatabase
class.