1

I'm trying to create a program which takes values that are doubles from the user and stores them in arrays before dividing them and rounding the answers. However I keep receiving errors trying to create double arrays.

 double n = sc.nextDouble();

    double a[] = new double[n];
    double b[] = new double[n];
    double roundedValues[] = new double[n];

    for (double i = 0; i < a.length; i++) {
        System.out.println("Please enter the values you would like to divide as pairs of two numbers: ");
        // Read pair and store it inside i-th position of a and b arrays 
        System.out.println("Enter first number: ");
        a[i] = sc.nextDouble();
        System.out.println("Enter second number you would like to divide by: ");
        b[i] = sc.nextDouble();
        roundedValues[i] = Math.round(a[i] / b[i]);
    }

The error I'm getting where I've declared the arrays says:

incompatible types: possible lossy conversion from double to int

5
  • 2
    What does an array with a double number of elements mean? I've never seen an array with 3.2734 elements. Commented Jul 9, 2017 at 20:40
  • 1
    Why are you using double as your counter (i)? Use int. Commented Jul 9, 2017 at 20:41
  • I've changed the control variable in the loop to int and that has solved that issue but i'm still getting the same error on the lines where I have declared the arrays. Can you help me understand why? I'd like to store values that are decimal numbers within the arrays I have created. Thanks Commented Jul 9, 2017 at 20:45
  • See Eran's comment. You're still creating a[] and b[] with a "double" number of elements. Anything in the the brackets needs to be an int. Commented Jul 9, 2017 at 20:47
  • OK. I'd like to store values that are decimal numbers within the arrays I have created, hence why I tried initialising the array as a double. I think I understand where I was going wrong, I thought that the int was responsible for the type of values that could be stored within the array. Is there a way I can store values that are of the type double within the array? Commented Jul 9, 2017 at 20:52

2 Answers 2

3

Firstly, variable n, defined as double n = sc.nextDouble(); is a double. You can't make an array whose length is a double because then you might end up with half an element. Imagine an array 4.5 elements long!

Define n like one of these instead:

int n = sc.nextInt()
or
int n = (int)sc.nextDouble();

Secondly, i is also a double! Define it like this:

for (int i = 0; i < a.length; i++) {

That way the compiler knows that i is an integer and doesn't have to worry about needing to retreive an element at a double index, like the 4.2th element! (which doesn't, and shouldn't exist)

Heres the fixed code:
int n = sc.nextInt();

double a[] = new double[n];
double b[] = new double[n];
double roundedValues[] = new double[n];

for (int i = 0; i < a.length; i++) {
    System.out.println("Please enter the values you would like to divide as pairs of two numbers: ");
    // Read pair and store it inside i-th position of a and b arrays 
    System.out.println("Enter first number: ");
    a[i] = sc.nextDouble();
    System.out.println("Enter second number you would like to divide by: ");
    b[i] = sc.nextDouble();
    roundedValues[i] = Math.round(a[i] / b[i]);
}
Sign up to request clarification or add additional context in comments.

Comments

1
a[i] and b[i] expect int type, but you supply double to it
Should be 
 for (int i = 0; i < a.length; i++) {

and

 double n = sc.nextDouble();
   should be changed to:
 int n = sc.nextInt();

3 Comments

The exception is coming from the fact that OP is using a double for n, and trying to declare that as the number of elements int he array declarations.
I've changed the control variable in the loop to int and that has solved that issue but i'm still getting the same error on the lines where I have declared the arrays. Can you help me understand why? I'd like to store values that are decimal numbers within the arrays I have created. Thanks
Change your n variable to an int and use nextInt() instead of nextDouble()

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.