0

I want to extract people born between two years.

Being year1 = "1990" and year2 = "2000". It seems that >= and <= doesn't work.

How can I compare these two date strings?

static void annesCom(Personne[] pers, int nbElem, String year1, String year2)
{
    int i;
    for(i=0; i < nbElem; i++)
        if(perso[i].year() >= year1 && perso[i].year() <= year2 )
    System.out.printf("Person %d) Born in %s\n", i, pers[i].year());
}
3
  • 3
    Convert to integer first... Commented Apr 21, 2015 at 16:38
  • 2
    Since we're actually more talking about dates, instead of numeric values, maybe use the Date (or a Date) class and use compareTo Commented Apr 21, 2015 at 16:41
  • possible duplicate of Comparing date strings in Java Commented Apr 21, 2015 at 16:47

5 Answers 5

4

You cannot use the <= and >= operators on Strings (or < and >). While you can use == and != on Strings, the results won't be what you expect.

For a numeric quantity, you shouldn't use the String datatype. In your Personne class, change the datatype of the variable that holds the year attribute to int, and the return datatype of the getter method year() to int as well. You can use <=, >=, <, >, ==, and != on int values.

In your annesCom method, accept int values for year1 and year2.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use compareTo method. But I suggest to parse string as integer with Integer.parseInt method and then do a comparison.

Comments

0

Parse the numbers currently contained in strings:

if(perso[i].year() >= Integer.parseInt(year1) && perso[i].year() <= Integer.parseInt(year2) )

The only comparison operator applicable to strings in Java is ==. This checks for reference equality (meaning it will only return true if the strings compared are in fact the same object). This will only yield true for strings with identical content known at compile time, as Java saves space by pointing them to the same object.

Comments

0

you can pass your String year into Integer.parseInt(), and then compare the integers with >= and <=

Comments

0

You cannot use >= or <= for a String value. String are different from other primitive values like int, double etc. You can convert String to integer and then compare it by using >= or <=

You modified code will look like this:

static void annesCom(Personne[] pers, int nbElem, String year1, String year2)
{
    int i;
    int year1Int = Integer.parseInt(year1);
    int year2Int = Integer.parseInt(year2);

    for(i = 0; i < nbElem; i++)
    {
        if(perso[i].year() >= year1Int && perso[i].year() <= year2Int)
        {
           System.out.printf("Person %d) Born in %s\n", i, pers[i].year());
        }
    }
}

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.