I am scanning a file of numbers into a datatype double array and am getting a type mismatch for my "score" and "i" variables that i have both already declared doubles.
static double[]convert(String file){
try {
File ar = new File(file);
Scanner sc = new Scanner(file);
double score = 0;
while(sc.hasNextDouble()) {
score ;
sc.nextDouble();
}
double[]stat= new double[score];
for(double i=0;i<stat.length;i ) {
stat[i]= sc.nextDouble();
}
return stat;
}
catch (Exception e) {
return null;}
}
CodePudding user response:
return stat;
should be after the for loop not inside it- you cannot initialize array with size that can be fraction so
double[] stat = new double[score];
is wrong, you should trydouble[] stat = new double[(int)score];
instead or make the variable calledscore
of typeint
stat[i]= sc.nextDouble();
is wrong because you made the variablei
of type double, so instead offor(double i=0;i<stat.length;i )
, you should tryfor(int i=0;i<stat.length;i )
as imagine if the variablei = 2.5
, there is nothing calledarr[2.5]
.