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?
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 :)
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. 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..
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.