Nakseung Nakseung - 7 months ago 8
Java Question

How to pick up only integer data from an ArrayList of String

I read a text file and put the data into an

ArrayList
of
String
.

The data is like

China 12351235123 Korea 123532523 USA 12341235123


I just need those integer data as an integer to find the first digit of the integers. I know how to find the first digit, but I don't know how I can pick up only the integer data.

Answer

I am assuming that your ArrayList is of type String. replaceAll() will take out anything that isn't 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);
}