-1

This is the Code:

private List listaPunktowInt(List lista) {
    
    String liniaString="";
    List<Integer> list=new ArrayList();
    Iterator<String> it=lista.iterator();
    while(it.hasNext()) {
        liniaString=it.next();
        
        if(Pattern.matches("\\d*",liniaString)) {
            list.add(Integer.parseInt(liniaString));
        }
    }
    
    //Collections.sort(list);
    //Collections.reverse(list);
    System.out.println(list);
    return list;
}

Input:[56, 4, 2 2022 04 06, 1, 1, 1]

Output:[56, 4, 1, 1, 1]

I want "2" to be included as well but the rest ("2022 04 06") to be ignored, is it possible?

  • 2
    Please [avoid raw types](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). The argument should be `List lista` and `list` should be declared/initialized as `List list = new ArrayList<>()`. – Joachim Sauer Sep 08 '22 at 13:18
  • *"I want "2" to be included as well but the rest ("2022 04 06")"* - Does it imply you need only the *first character* of every string or this applies only to strings representing a date? Why there's no issue with `56`? – Alexander Ivanchenko Sep 08 '22 at 13:21
  • @AlexanderIvanchenko I want all characters before space to be a single int. The "2" or "56" represent a points and the rest date. – molyyyllom Sep 08 '22 at 13:26
  • What if one of the strings is `"123foo"`? Or just `"foo"`? Or the empty string `""`? How should the code behave then? – Joachim Sauer Sep 08 '22 at 13:26

1 Answers1

0

Right now Pattern.matches checks if the whole string matches \d* (i.e. only contains digits). That's very easy with the static Pattern.matches method.

To do anything more involved with regex, you should use a Matcher (which is the thing that is about a single use of a regex, i.e. it's the combination of a pattern like \d* and some input string like liniaString). It' still used under the hood, but Pattern.matches hides that fact from you.

So your code could look like this (I also fixed the types and added static since the method doesn't depend on any state of the object):

private static List<Integer> listaPunktowInt(List<String> lista) {
    Pattern pattern = Pattern.compile("^\d+");
    List<Integer> list=new ArrayList<>();
    for (String liniaString : lista) {
        Matcher matcher = pattern.matcher(liniaString);
        if(matcher.find()) {
            list.add(Integer.parseInt(matcher.group()));
        }
    }
    return list;
}

Right now this simply takes as many digits from the beginning as possible and ignores whatever comes after them (characters, space, end of string, ...). If you want something other than that, you can adapt the pattern used.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
  • @molyyyllom: you're welcome. Note that it's not necessary to write individual thank you comments on SO. It's sufficient to upvote answers (and questions!) that help you. If you want you can also accept answers. Doing either of that is actually preferred to writing "thank you" comments. – Joachim Sauer Sep 08 '22 at 13:55