Home > Software design >  I am trying to return an array of doubles and getting a type mismatch
I am trying to return an array of doubles and getting a type mismatch

Time:10-02

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:

  1. return stat; should be after the for loop not inside it
  2. you cannot initialize array with size that can be fraction so double[] stat = new double[score]; is wrong, you should try double[] stat = new double[(int)score]; instead or make the variable called score of type int
  3. stat[i]= sc.nextDouble(); is wrong because you made the variable i of type double, so instead of for(double i=0;i<stat.length;i ), you should try for(int i=0;i<stat.length;i ) as imagine if the variable i = 2.5, there is nothing called arr[2.5].
  • Related