Search code examples
javaarraysmultidimensional-arrayjava-10

Type mismatch for Math.pow and operator undefinded - Java


I'm working on an assignment to calculate trajectory based on speed and angles and I'm getting a couple of errors in my calculation that I don't understand how to fix. The program works like this: reads in values from a text file into 2 int arrays using nextInt() method. These arrays are passed to a class where the calculateTrajectory method and are combined into a 2D array. These are all int arrays. In the calculateTrajectory, after combining into a 2D array, I then calculate the trajectory, but get errors in my caclulation.

Descriptions of the errors I'm getting:

As I'm looping through the 2D array to do my calculation, I'm getting one error when using the Math.pow method on my speed value. That error says "the method pow(double, double) in the type Math is not applicable for the arguments (int[], int)". The advice is to change type of speed to double. Do I need to cast the values to doubles?

The second error in the calculation comes when I try to multiply angles by 2 inside the toRadians. The error I get here is "The operator * is undefined for the argument type(s) int, int[]". There is no quick fix advice here. I've looked up this error and any answers I've seen are really confusing.

I've tried making my arrays double arrays with no avail. I'm wondering if I can cast the values of the int arrays into doubles when I populate my 2D array, if that will solve the problem.

public void calculateTrajectory(int[][] speed, int[][] angles)
    {
        //convert speed and angles arrays into one 2D array
        for(int row = 0; row < speed.length; row++)
        {
            for(int col = 0; col < angles.length; col++)
            {
                combined[row] = speed[row];
                combined[col] = angles[col];
            }
        }

        //calculate the trajectory from 2D array
        for(int r = 0; r < speed.length; r++)
        {
            for(int c = 0; c < angles.length; c++)
            {
                trajectory = (Math.pow(speed[r], 2)) * 
                    Math.sin(Math.toRadians(2 * angles[c])) / 32.1713;
            trajectoryArray[r] = trajectory;
            }
        }
    }

I know, based on the equation, that I will get doubles as a result of the calculation. The variable that I've set up to take the result of the calculation is a double. I want to be able to run the calculation without the errors, while looping through and populating my trajectoryArray with the results.


Solution

  • For the question

    1. Math.pow(double, double) takes 2 double arguments. But here speed[r] is an array as speed is a 2 dimensional array and 2 is an integer. you should use speed[r][c]. for int 2 you can say 2.0.

    2. Math.toRadians(double) requires a double value but 2 * angles[c] is not a double value and cannot be calculated as angles[c] is an array since angles is a two dimensional array. you can do 2.0 * angles[r][c]