[英]type mismatch: can't convert from double to int java
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. 我有这个java代码。 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. 我想从文件中获取数字并将其转换为double,但这给了我一个错误:类型不匹配:无法从double转换为int。 How can I fix this? 我怎样才能解决这个问题?
Since N is double
You need a type case there 由于N是double
您需要一个类型大小写
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 :) 原因是double
是浮点类型,因此无法创建length
1.5的数组:)
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();
}
}
}
N is a double but you're attempting to iterate over it. N是一个双精度型,但您正在尝试对其进行迭代。 You must type cast it. 您必须输入强制转换。
your problem is here.. 你的问题在这里..
double L=Double.parseDouble(pole[0]);
double N=Double.parseDouble(pole[1]);
change to 改成
int L=Integer.parseInt(pole[0]);
int N=Integer.parseInt(pole[1]);
because int
only accept is array index.. 因为int
只接受数组索引。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.