Question:
your function should take a string and print only the numbers contained in the string.
i/p : String inputString = "Sekhar124hekkkoer9239939siiipere23+++33!!!!!!";
o/p: [124, 9239939, 23, 33]
Code:
your function should take a string and print only the numbers contained in the string.
i/p : String inputString = "Sekhar124hekkkoer9239939siiipere23+++33!!!!!!";
o/p: [124, 9239939, 23, 33]
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | public class OnlyIntegers { public static void main(String[] args) { String inputString = "Sekhar124hekkkoer9239939siiipere23+++33!!!!!!"; List numbers = new ArrayList(); char[] temp = null; char[] in = inputString.toCharArray(); int tempIndex = 0; for (char c : in) { if (temp == null) { temp = new char[10]; } int ascii = (int) c; if (ascii > 47 && ascii < 58) { temp[tempIndex++] = c; } else { if ((new String(temp).substring(0,tempIndex).length()) > 0) { numbers.add((new String(temp).substring(0,tempIndex))); temp = null; tempIndex=0; } } } System.out.println(numbers); } } |
No comments:
Post a Comment