Tuesday, January 12, 2016

print only the number's from a give String

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:

 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:

AWS certification question

AWS AWS Hi! this is for questions related to AWS questions. EC2 instances EC2 storage types cold HDD : 1. Defines performance in terms...