[英]split String not working in java can't import `split`
I am trying to split a string as the code below 我试图拆分字符串作为下面的代码
String []data = {"3.5,2.3,4.2,5.4,7.4,2.7"};
String s[] = data.split("\\,");
double point3[] = new Double [s.length];
double allPoint[] = new double [s.length];
for (int i = 0; i < s.length; i++){
point3[2] = Double.parseDouble(s[2]);
//lng[i] = Double.parseDouble(s[i]);
allPoint[i] = Double.parseDouble(s[i]);
}
I also tried with data.split(",");
我也尝试过
data.split(",");
But the problem is not with backslashes, it gives error at split
and hint shows that 但问题不在于反斜杠,它在
split
时提供错误,提示显示
cannot find symbol, symbol: method split(String)
找不到符号,符号:方法split(String)
I'm unable to import split
what can I do now. 我无法导入
split
我现在可以做什么。
Here data
represent a string array. 这里的
data
代表一个字符串数组 And data present in 0
position. 并且数据存在于
0
位置。 For getting data from data array used data[0]
. 用于从数据数组中获取数据使用
data[0]
。
This code should work for you : 此代码应该适合您:
String []data = {"3.5,2.3,4.2,5.4,7.4,2.7"};
String s[] = data[0].split("\\,");
double allPoint[] = new double [s.length];
for (int i = 0; i < s.length; i++){
System.out.println(s[i]);
}
Output :
3.5
2.3
4.2
5.4
7.4
2.7
The above solution is correct But it can also be done with array as data[0].split(",");
上面的解决方案是正确的但它也可以用数组作为
data[0].split(",");
. 。
Because in case of array data is at 0th index and we can split it with its index value. 因为如果数组数据是第0个索引,我们可以用它的索引值拆分它。
And if you use this: 如果你使用这个:
double point3[] = new Double [s.length];
double point3 [] = new Double [s.length];
it means you are making object of double
because Double
with capital D
indicate to object. 这意味着你正在制作
double
对象,因为带有大写D
Double
表示对象。 your allpoint[]
array may work correctly. 你的
allpoint[]
数组可能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.