0

I am getting data from a Bluetooth characteristic in bytes and converting it into an array of floats that look like this:

[318.0159, 331.81818, 324.71603, 348.4345, 323.108, 3.2360008]

I want to be able to split this data into 6 strings,

"318.0159" "331.81818" "324.71603" "348.4345" "323.108" "3.2360008".

I have already tried to do this:

EDIT(charData initialized like so):

final StringBuilder stringBuilder = new StringBuilder(bytes.length);
for (byte byteChar : bytes)
    stringBuilder.append(String.format("%02X ", byteChar));
String charData = stringBuilder.toString();
String[] data = charData.split(",");
System.out.println(data[1]);
System.out.println(data[2]);
System.out.println(data[3]);
System.out.println(data[4]);
System.out.println(data[5]);
System.out.println(data[6]);

but when it tries to print the first data point, I get the exception:

Unhandled exception in callback
java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

Thanks in advance for the help!!

4
  • how you initialize charData? Commented Aug 2, 2018 at 15:27
  • Array indexes are zero based. Start from 0 Commented Aug 2, 2018 at 15:29
  • Did you try to print charData before splitting it? Do it and see if it is like the array you posted in your question. Commented Aug 2, 2018 at 15:36
  • You built a string with no commas in it, then called split(",") on it. Also, there are no float values in your code. Commented Aug 2, 2018 at 15:52

4 Answers 4

2

You need remove [ and ] first, and the array index ranges [0, 5]:

String charData = "[318.0159, 331.81818, 324.71603, 348.4345, 323.108, 3.2360008]";
charData = charData.replace("[", "").replace("]", "");

String[] data = charData.split(",");
System.out.println(data[0]);
System.out.println(data[1]);
System.out.println(data[2]);
System.out.println(data[3]);
System.out.println(data[4]);
System.out.println(data[5]);

output:

318.0159
 331.81818
 324.71603
 348.4345
 323.108
 3.2360008

To avoid the leading white spaces, you can split with regex:

String[] data = charData.split(",\\s*");

output:

318.0159
331.81818
324.71603
348.4345
323.108
3.2360008
Sign up to request clarification or add additional context in comments.

1 Comment

It doesnt seems to be issue with data[6] because error is not java.lang.ArrayIndexOutOfBoundsException: index=6
2

Sun's answer is spot on, I prefer to trim input as well:

charData= charData.replace("[", "").replace("]", "");
String[] data = charData.split(",");
for(int x=0;x<=data.length;x++){
    String data_cleaned = data[x].trim();
    System.out.println(data_cleaned);
 }

2 Comments

Your answer worked very well for me as well, thanks for the help!
Glad I could help :)
1

try this

String[] data = ["318.0159", "331.81818", "324.71603", "348.4345", "323.108", "3.2360008"]
for (int i = 0; i < data.size(); i++) {
     System.out.println(data[i]);
}

ArrayIndexOutOfBoundsException occurs when you access some sort of array but the index is not available. also remember that an array starts from 0

Comments

0

It seems your charData don't have any data,
It is not same as you have asked in querstion.
Please print the data before split()
Also check data in bytes before your for (byte byteChar : bytes)

Also it is not the issue of array index o to 5 as others suggested, other wise error would have been java.lang.ArrayIndexOutOfBoundsException: index=6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.