user5508041
user5508041

Reputation:

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(","); But the problem is not with backslashes, it gives error at split and hint shows that

cannot find symbol, symbol: method split(String)

I'm unable to import split what can I do now.

Upvotes: 3

Views: 1701

Answers (3)

Inzimam Tariq IT
Inzimam Tariq IT

Reputation: 6758

The above solution is correct But it can also be done with array as data[0].split(",");.

Because in case of array data is at 0th index and we can split it with its index value.

And if you use this:

double point3[] = new Double [s.length];

it means you are making object of double because Double with capital D indicate to object. your allpoint[] array may work correctly.

Upvotes: 0

Shiladittya Chakraborty
Shiladittya Chakraborty

Reputation: 4418

Here data represent a string array. And data present in 0 position. For getting data from data array used 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

Upvotes: 0

ljleppan
ljleppan

Reputation: 366

The method split() belongs to String, not to 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"; instead.

Upvotes: 6

Related Questions