简体   繁体   English

split不能在java中工作的字符串无法导入`split`

[英]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我现在可以做什么。

The method split() belongs to String , not to Array . 方法split()属于String ,而不属于Array To make this work, you must define your data as String data = "3.5,2.3,4.2,5.4,7.4,2.7"; 要使其工作,必须将数据定义为String data = "3.5,2.3,4.2,5.4,7.4,2.7"; instead. 代替。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM