0

I am trying to make my method display people who are born during the month of July.

class Personne {
    private String naissance;
    private int nbCafe;

    public Personne(String year, int number) {
        naissance = year;
        nbCafe = number;
    }

    public Personne(String year) {
        naissance = year;
        nbCafe = 1;
    }

    public String getNaissance() {
        return naissance;
    }

    public int getNbcafe() {
        return nbCafe;
    }

    public void afficher(String message) {
        System.out.println(message + ": nee le 16 novembre 1994, consomme 2 tasse(s) de cafe");
    }

    static void afficherTable(Personne[] pers, int amount) {
        System.out.printf("\nContenu du tableau de %d personne(s)\n", amount);
        System.out.printf("nbPers    Birth        numCafe\n");
        for (int i = 0; i < amount; i++)
            System.out.printf(" %d)     %s      %d\n", i, pers[i].getNaissance(), pers[i].getNbcafe());
    }

    static void demo1(Personne[] pers, int amount) {
        int count = 0;

        String juillet = "07";
        for (int i = 0; i < amount; i++)
            if (pers[i].toString().substring(3, 5) == juillet) {
                count++;
            }
        System.out.println(count);
    }
}

public class popo {
    public static void main(String args[]) {

        Personne p1 = new Personne("16/11/1994", 2);
        Personne p2 = new Personne("15/12/1990");

        Personne[] pers = {new Personne("12/10/1991", 3),
                new Personne("15/10/1990", 6),
                new Personne("13/07/1993", 3),
                new Personne("05/06/1991"),
                new Personne("16/12/1992", 3)};
        int nbpers = pers.length;

        p1.afficher("Informations de p1");
        Personne.afficherTable(pers, nbpers);

        Personne.demo1(pers, nbpers);
    }
}

The method demo1() in my class is supposed to pick out the people who are born in July but it doesn't seem to work. I've tried using charAt/indexOf/substring to get the month from "Naissance" to no avail. Is there another way of finding what you want from a table of strings?

Morfic
  • 15,178
  • 3
  • 51
  • 61
  • 1
    Compare Strings with equals instead of == – fabian Apr 25 '14 at 15:00
  • also you want to use getNaissance rather than pers[i].toString().substring(3,5). You should probably just write a method to get the month, perhaps returning it as a int instead of a string – Marshall Tigerus Apr 25 '14 at 15:01

5 Answers5

1

I would add this to the Personne class:

public int getMonth(){
     return Integer.parseInt(this.naissance.substring(3,5));
}

Then you can call

If (pers[i].getMonth == 7)
Marshall Tigerus
  • 3,675
  • 10
  • 37
  • 67
1

The problem is that you're using:

pers[i].toString().substring(3, 5) == juillet)

You need to use

pers[i].getNaissance().substring(3, 5).equals(juillet))

since you're looking for the month from the naissance field.

You should use the String.equals(String other) function to compare strings, not the == operator.

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. More info: Java String.equals versus ==

Community
  • 1
  • 1
Joshua Kissoon
  • 3,269
  • 6
  • 32
  • 58
  • Even though it's one of the basics, I'd find it useful if you could explain why use `equals` instead of `==` (just my 2 cents) – Morfic Apr 25 '14 at 15:12
0
static void demo1(Personne[] pers, int amount){
    int count = 0;
    final String juillet = "07";
    for (int i=0; i<amount; i++)
        if (pers[i].naissance.substring(3,5).equals(juillet)) {
            count++;
        }
        System.out.println(count);
   }
}

The toString method was not overriden, you intended to use getNaissance() or so. Also comparing strings is done with equals, not ==. Instead of amount you may use pers.length.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0

if(pers[i].toString().substring(3,5)== juillet) has to be

if(pers[i].toString().substring(3,5).equals(juillet))
Arkillon
  • 51
  • 4
0

Honestly I'ld do in this way:

static  void demo1(Personne[] pers, int amount) throws ParseException{
int count=0;
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sdf.setLenient(true);
Calendar c = GregorianCalendar.getInstance();
int juillet=7;
for (int i=0; i<amount;i++)
{

    Date d = sdf.parse(pers[i].getNaissance());
    c.setTime(d);
    int month = c.get(Calendar.MONTH)+1;
    if( month == juillet )
    {
        count++;
    }
}
System.out.println(count);
}

Angelo

Angelo Immediata
  • 6,635
  • 4
  • 33
  • 65