I have this code in which I want to convert a String (e.g. [15, 52, 94, 20, 92, 109]) to an Array/ArrayList.
I have tried this code:
ArrayList<Byte> sdata = new ArrayList<>();
String bytessonvert = new String();
boolean run = true;
System.out.println("Enter Data: ");
String bytes = input.nextLine();
Bytes = bytes.substring(1, bytes.length());
for (int i = 0; i < bytes.length(); i++) {
if (Objects.equals(bytes.substring(i, i), " ")) {
sdata.add((byte) Integer.parseInt(bytessonvert));
bytessonvert = "";
} else if (bytes.substring(i, i) == ",") {
sdata.add((byte) Integer.parseInt(bytessonvert));
bytessonvert = "";
} else {
bytessonvert = bytessonvert + bytes.substring(i, i);
}
}
"a, b, c".split(",", -1)will result in an array likenew String[] { "a", " b", " c"}- usingsplit("\\h*,\\h*", -1)it will even eliminate eventual spaces before and after the,||-1to avoid removing trailing empty strings