Why do not you use:
public static void main(String[] args) {
String strings = "one*10*two*11*three*12";
String[] spl = strings.split("\\*");
ArrayList<Integer> arrli = new ArrayList<>();
List<String> al = new ArrayList<>();
for (String s : spl) {
if (s.matches("\\d+")) {
arrli.add(Integer.parseInt(s));
} else {
al.add(s);
}
}
System.out.println(al);
System.out.println(arrli);
}
This will help you if you have two consecutive intsor String, like this:
"one*9*10*two*11*three*four*12"
//--^---^---------^------^
EDIT
Now I have my input like "astv * 12atthh124ggh * dhr1234sfff123 * dgdfg1234 * mnaoj" I need to separate the string and the numbers separately
In this case, you should use templates, for example:
String str = "astv*12atthh124ggh*dhr1234sfff123*dgdfg1234*mnaoj";
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(str);
List<String> strings = new ArrayList<>();
List<Integer> nums = new ArrayList<>();
while (m.find()) {
nums.add(Integer.parseInt(m.group()));
}
p = Pattern.compile("[a-z]+");
m = p.matcher(str);
while (m.find()) {
strings.add(m.group());
}
System.out.println(nums);
System.out.println(strings);
results
[12, 124, 1234, 123, 1234]
[astv, atthh, ggh, dhr, sfff, dgdfg, mnaoj]
YCF_L source
share