For example, I have input String: “qwerty1qwerty2“;
As Output I would like have [1,2].
My current implementation below:
import java.util.ArrayList;
import java.util.List;
public class Test1 {
public static void main(String[] args) {
String inputString = args[0];
String digitStr = "";
List<Integer> digits = new ArrayList<Integer>();
for (int i = 0; i < inputString.length(); i++) {
if (Character.isDigit(inputString.charAt(i))) {
digitStr += inputString.charAt(i);
} else {
if (!digitStr.isEmpty()) {
digits.add(Integer.parseInt(digitStr));
digitStr = "";
}
}
}
if (!digitStr.isEmpty()) {
digits.add(Integer.parseInt(digitStr));
digitStr = "";
}
for (Integer i : digits) {
System.out.println(i);
}
}
}
But after double check I dislake couple points:
-
Some lines of code repeat twice.
-
I use List. I think it is not very good idea, better using array.
So, What do you think?
Could you please provide any advice?
Use replaceAll:
Output:
[EDIT]
If you want to include
-a.e minus, add-?:Output:
Description
+Between one and unlimited times, as many times as possible, giving back as needed-?One of the characters “-?”0-9A character in the range between “0” and “9”