Nakseung Nakseung - 7 months ago 10
Java Question

Java: how to pick up only integer data from an ArrayList of String

the data in the ArrayList of String is like


China 12351235123

Korea 123532523

USA 12341235123


Is there a way to pick up only integers?

Answer

I think should answer your question. I am assuming that your ArrayList is of type String and .replaceAll will take out anything that isnt a number(0-9)

 ArrayList<String> countrys = new ArrayList<String>();
 countrys.add("China 12351235123");
 countrys.add("Korea 123532523");
 countrys.add("USA 12341235123");

    for(String c:countrys){
        String number = c.replaceAll("[^0-9]", "");
        System.out.println(number);
    }