0

I don't understand why i'm getting these errors when i compile my code.

Error: F:\G\programA: operator * cannot be applied to int,double[]

Error: F:\G\programA: operator - cannot be applied to double,double[]

import java.io.*;

public class programA
{
  public static void main (String [] args) throws IOException
  {
    BufferedReader myInput = new BufferedReader (new InputStreamReader (System.in));

  int points = 100, dimension = 3;

  double [] length;
  length = new double [dimension];

  double [][] loc;
  loc = new double [points][dimension];

   for (int x = 0; x < points; x++){
  for (int y = 0; y < dimension; y++){
   location [x][y]= (Math.random() * (2 *length)) - length;
  }
  }

  }
}

4 Answers 4

3
2 *length

You're multiplying an array by an int. Not going to happen. The code around that is somewhat unclear, but since you're trying to set a single array element you'll want to get a single element of length, such as by length[0] or length[someIntInRange].

Also, location [x][y] should be loc[x][y] as there is no field called location.

Sign up to request clarification or add additional context in comments.

Comments

1

Since the code isnt clear, I am assuming you need

loc [x][y]= (Math.random() * (2 *length[y])) - length[y];

Math Operations cannot be done between an array and a number.

1 Comment

more likely he needs length[y], not length[dimension] which will get an out-of-bounds exception
0

I don't understand why i'm getting these errors when i compile my code.

Error: F:\G\programA: operator * cannot be applied to int,double[]

Error: F:\G\programA: operator - cannot be applied to double,double[]

You are trying to multiply an int and later a double against an array of type double. This is not possible. If you want to multiply against all values in the array, you need to iterate over it to do so.

Comments

0

Your problem is an undefined operation here...

double [] length;
length = new double [dimension];
...
(2 *length)

You need to select a value from the length array.

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.