type mismatch: can't convert from double to int java
13,069
Solution 1
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 :)
Solution 2
Try with this
public class Tuple {
public static void main(String[] args) {
try{
BufferedReader br=new BufferedReader(new FileReader("UserController.txt"));
String[] pole=br.readLine().split(" ");
double L=Double.parseDouble(pole[0]);
double N=Double.parseDouble(pole[1]);
System.out.println("L is "+L);
String[] weight=new String[(int) N];
double[] scale=new double[(int) N];
double[] place=new double[(int) 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;
}
}
catch(Exception e) {
e.printStackTrace();
}
}
}
Author by
user2983594
Updated on June 15, 2022Comments
-
user2983594 about 2 years
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?
-
Antoniossss over 10 yearsAnd why would you want to truncate your data insteed of rounding it?
-
Suresh Atta over 10 years@Antoniossss True, but there is no point to rounding. I never create an element if the value 0.9 or 0.51 :) Both are same in this case.