I am writing a program to perform various calculations using vector functions, but the program that I currently limit to negative numbers. I tried using different delimiters, but I can not find the correct option.
Does anyone know how to keep positive and negative digits when splitting a string? Also, is there a way to store decimal values? .45 will return 45 and .29 will return 29
This is the code:
ArrayList<Integer> list = new ArrayList<Integer>();
String twoVectors = "a=<-1,2,-3> b=<4,-5,6>";
String[] tokens = twoVectors.split("\\D");
for (String s : tokens)
if (!s.equals(""))
list.add(Integer.parseInt(s));
System.out.println(Arrays.toString(list.toArray()));
When I run the program, I get [1, 2, 3, 4, 5, 6] instead of [-1, 2, -3, 4, -5, 6]. All the functions that I worked fine, but do not work when using negative values.
Any help would be appreciated.