0

I would like to get the numbers that start with 2003 (2003001 and 2003002) and put them into another list.

public static void main(String[] args) {

    String [] num = {"2012001", "2003001", "2003002"};

    List<String> list = new ArrayList<String>();
    for(String actualList : num) {
        list.add(actualList);
    }
}
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46
Rho
  • 29
  • 5
  • [String.startsWith()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith(java.lang.String)) – Omkar76 Jun 05 '22 at 10:57
  • Are those dates? Looks to me like day 001 of year 2012 and so forth. In that case handle them as `LocalDate` objects, not as strings. And use `LocalDate.getYear()` to get the year (equal to the first 4 digits of your example strings). See for example [how to convert Julian date (ordinal date) in Java \[duplicate\]](https://stackoverflow.com/questions/61494889/how-to-convert-julian-date-ordinal-date-in-java). – Ole V.V. Jun 05 '22 at 11:47
  • 1
    Actually, year and the registered number. Example people born in 2012. The first one to register who was born in 2012 is 2012-001 then the second one to register is 2012-002 and so on. I am actually making an ID number on firebase using the date they were born. – Rho Jun 06 '22 at 03:17
  • Thanks for answering. In that case I would probably start out by designing a class to hold the number (or the person, depending) and being responsible for returning the birth year (as a `Year` object and/or as an `int`). Strings may seem versatile enough but really are unsuited for holding structured data like these. – Ole V.V. Jun 06 '22 at 06:54

3 Answers3

3

You can use the startsWith() method:

String[] num = {"2012001", "2003001", "2003002"};

List<String> list = new ArrayList<>();

for (String number : num) {
    if (number.startsWith("2003")) {
        list.add(number);
    }
}
Adixe
  • 119
  • 7
2

Besides @Adixe iterative solution, you could concisely achieve that also with streams by streaming the array, filtering for the elements starting with 2003 and the collecting the remaining elements with the terminal operation collect(Collectors.toList()).

String[] num = {"2012001", "2003001", "2003002"};
List<String> listNums = Arrays.stream(num)
        .filter(s -> s.startsWith("2003"))
        .collect(Collectors.toList());
Dan
  • 3,647
  • 5
  • 20
  • 26
1

As @Ole V.V. has pointed out in the comments, it seems like strings in your array are comprised of the year like 2012 and the day of the year like 001.

If so, it would be match more convenient to convert this data into LocalDate than operating with it as if it's a plain string.

To parse these row strings into LocalDate you need to create a DateTimeFormatter using the static method ofPattern().

A string pattern that corresponds to the sample data will be the following:

yyyyDDD

y - stands for year;

D - day of the year.

For more information see

So to filter out dates having a particular year, firstly we need to parse every string using LocalDate.parse() by passing the string and the formatter as arguments, and then extract year from each date by applying getYear():

String [] num = {"2012001", "2003001", "2003002"};
        
int targetYear = 2003;
    
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyDDD");
        
List<LocalDate> dates = Arrays.stream(num)
    .map(date -> LocalDate.parse(date, formatter)) 
    .filter(date -> date.getYear() == targetYear)
    .collect(Collectors.toList());
    
System.out.println(dates);

Output:

[2003-01-01, 2003-01-02]
Alexander Ivanchenko
  • 25,667
  • 5
  • 22
  • 46