float data[] = new float[j]; why isn't this valid in java ? Cant we have an array in float type? -


when compile error

solution.java:13: error: possible loss of precision         float data[] = new float[j];                                  ^ 

required: int found: float

got in following code

import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*;  public class solution {  public static void main(string[] args) {         scanner in = new scanner(system.in);         float sum =0;         float j = in.nextfloat();         float data[] = new float[j];         for(int i=0;i<j;++i)             {             float m = in.nextfloat();              data[i] = m;         }         for(int k =0; k<j;++k)             {              sum += data[k];         }     system.out.println(sum); } 

}

may wrong, new learner please bear noobness.

the index of array must int. can't have array 17.54 elements.

if wish length of array determined value of float variable, can cast int (assuming j not large) :

float data[] = new float[(int)j]; 

Comments

Popular posts from this blog

mysql - FireDac error 314 - but DLLs are in program directory -

git - How to list all releases of public repository with GitHub API V3 -

c++ - Getting C2512 "no default constructor" for `ClassA` error on the first parentheses of constructor for `ClassB`? -