Depending on your needs, it looks like something that could be solved using regex or string splitting by a regex number pattern then filtering out what you need.
Example of doing the first line for matching 1 and 3
String s = "Z=1X1+3X2";
Pattern p = Pattern.compile("([13])");
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group());
}
or via split
String[] split = s.split("\\D");
// for every item in array, if 1 or 3 do something with it..
3
solved Separate Int from String on file input [closed]