-2

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.

3
  • 2
    Please show us what you've tried so far. Commented May 1, 2016 at 8:37
  • ArrayList of String won't accept Integers. It's may be ArrayList of Objects. Is it ? OR "China 12351235123" is a single String ? Commented May 1, 2016 at 8:39
  • Maybe This can help Commented May 1, 2016 at 8:43

4 Answers 4

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much! that can be a way!
I don't need those string data, but I just need integer data to find a first digit of them.
1

Try this :

  // Creation of ArrayList of string
  ArrayList<String> obj = new ArrayList<String>();

  // arrayList of string
  obj.add("China 12351235123");
  obj.add("Korea 123532523");
  obj.add("USA 12341235123");


    Iterator<String> iterator = obj.iterator();
    String str;
    String[] split_str;
    long  value;
    while (iterator.hasNext()) {
        str = iterator.next().toString();
        split_str = str.split(" ");
        String split_2 = split_str[1];
        value = Long.parseLong(split_2);
        System.out.println(value);
    }

Comments

0

You can use Regex as SLY suggested, or you can go over your list and split the object into two parts: 'country' and 'number' using:

for(String s : array){
    String[] parts = s.split(" ");
    long number = Long.parseLong(parts[1]);
    System.out.println(number);
}

NOTE: the invariant is that each element contains two parts with the delimiter 'white space', and the second part is a number.

1 Comment

I don't need those string data, but I just need integer data to find a first digit of them .
0

You can use regex and would be careful that not all that numbers can be in a integer range... so there for the list of longs in the code below

Example:

public static void main(String[] args) {
    List<Long> longList = new ArrayList<Long>();
    Pattern pattern = Pattern.compile("\\w+([0-9]+)");// regex pattern
    for (String string : listOfCountriesAndNumbers) {
        Matcher matcher = pattern.matcher(string);
        for (int i = 0; i < matcher.groupCount(); i++) {
            matcher.find();
            longList.add(Long.parseLong(matcher.group()));
        }
    }
    System.out.println(longList);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.