type mismatch: can't convert from double to int java

user2983594 picture user2983594 · Nov 23, 2013 · Viewed 11.2k times · Source
public static void main(String[] args) {
try{
    BufferedReader br=new BufferedReader(new FileReader("file.txt"));

    String[] pole=br.readLine().split(" ");
    double L=Double.parseDouble(pole[0]);
    double N=Double.parseDouble(pole[1]);
    String[] weight=new String[N];
    double[] scale=new double[N];
    double[] place=new double[N];
    for(int i=0; i<N; i++){
        weight[i]=br.readLine();
        String[] variable=weight[i].split(" ");
        double variable1=Double.parseDouble(variable[0]);
        double variable2=Double.parseDouble(variable[1]);
        scale[i]=variable2;
        place[i]=variable1*variable2;               
    }

I have this java code. I Want to get the number from the file and convert them into double, but it gives me that error: type mismatch: can't convert from double to int. How can I fix this?

Answer

Suresh Atta picture Suresh Atta · Nov 23, 2013

Since N is double You need a type case there

String[] weight=new String[(int)N];

The reason is double is a floating point type and you cannot create an array of length 1.5 :)