d_johnstone
d_johnstone

Reputation: 13

how to call a method through a 2d array error

I'm trying to create a new list so that whatever the user enters into the altitude this method should look for the coordinates and return the coordinates below what user has entered. I'm struggling to get this method to run.

static double[][] array;
Map m = new Map();

public static void main(String[] args) throws FileNotFoundException {
    m.coordinatesBelow(-2000)
}

public static void readDataArray(String filename) throws FileNotFoundException {
    Scanner input = new Scanner(new BufferedReader(new FileReader("sample.xyz")));
    int rows = 2500000;
    int columns = 3;

    double[][] array= new double[rows][columns];
    int i = 0;
    while (input.hasNextLine()) {
        String[] line = input.nextLine().trim().split("\t");
        for (int j = 0; j < line.length; j++) {
            array[i][j] = Double.parseDouble(line[j]);
        }
        i++;
    }
    System.out.println(Arrays.deepToString(array));
}

public List<Double> coordinatesBelow(double altitude) {
    List<Double> coordinatesBelow = new ArrayList<>();
    for (int i = 0; i < array.length; i++) {
        coordinatesBelow.add(array[i][2]);
    }
    coordinatesBelow.removeIf(b -> b > altitude);
    return coordinatesBelow;
}

Upvotes: 0

Views: 78

Answers (2)

Kirill Simonov
Kirill Simonov

Reputation: 8491

Your static double[][] array; declared at the first line and double[][] array = new double[rows][columns]; are 2 different variables: the first is static, the second is local. So when you read the values into the local array the static one remains empty.

If you want to save the values into the static array, don't create a new variable, just initialize the static array in your static method - change

double[][] array = new double[rows][columns];

to

array = new double[rows][columns];

UPD: If you are getting a NullPointerException at the line for (int i = 0; i < array.length; i++), the only thing that can be null here is array. So make sure the array is initialized before you call coordinatesBelow. And you initialize it in the readDataArray method, so try calling it first:

public static void main(String[] args) throws FileNotFoundException {
    readDataArray("filename.txt");
    m.coordinatesBelow(-2000);
}

Bu the way, it looks like the filename argument is not used anywhere, so you can remove it.

Upvotes: 1

Harry Jones
Harry Jones

Reputation: 342

You're calling coordinatesBelow as a method of Map. Is this correct? This would need to be a class that you have created yourself like so:

private static class Map {
        public List<Double> coordinatesBelow(double altitude) {
            List<Double> coordinatesBelow = new ArrayList<>();
            for (int i = 0; i < array.length; i++) {
                coordinatesBelow.add(array[i][2]);
            }
            coordinatesBelow.removeIf(b -> b > altitude);
            return coordinatesBelow;
        }
}

Upvotes: 0

Related Questions