Return all birthdays of the month chosen by the user

Asked

Viewed 227 times

1

I’m developing a system of an agenda of friends. Some classes have been created: Endereço, Amigo, GerenciarAmigo.

When I put the system to run, it doesn’t show me the String of the birthday names of that month, but only one person.

The method I did was this:

//Metodo que recebe um mês e retorna todos os amigos aniversariantes

public static String aniversariarNoMes(int mes) {
    String saida = "";

    //Faz a validação do mes escolhido
    if (mes < 01 || mes > 12) {
        return "Mês invalido.";
    } else {
        //Percorre o ArrayList a procura do mes informado
        for (Amigo amigo : agenda) {
            if (mes == amigo.getMesNas()) {
                saida = amigo.getNome() + "\n";

                return saida; //retorna os nomes encontrados
            }

            return saida+=amigo.getNome()+"\n";
        }
    }

    return null;
}
  • Please, when you paste code into Stackoverflow, enter 4 characters before each line of code. This way, the code is readable.

  • 2

    I’m new here, so I don’t know very well. But I’ll pay attention, thank you.

  • 2

    But if the month of birth of amigo is what you want, you update the value of saida and immediately returns it, leaving the method. Wouldn’t the correct one be to concatenate the value and only in the end return? By the way, if you’re new to the community, start by doing the [tour].

  • @The correct thing is that I’m new to programming and I’m looking for help. This is why I joined the community.

  • And we’re here to help. You’ve done the table test to understand what your code does?

  • I didn’t @Andersoncarloswoss

  • If using Calendar to catch the month, it does not count from 1 to 12 but from 0 to 11. In addition, your code is confused, things are missing there to understand better, as this class Friend and as you call this method.

  • @Andersoncarloswoss can post all the code?

  • I do not think strictly necessary, because the logic error is clear already in the code posted. If you take the table test, it might be clear to you as well. Can you do it? Now, here we have excellent Java programmers. Maybe if you post the full code, you might get hints for better implementations if that’s the case.

  • How does the table test?

Show 5 more comments

2 answers

3

Let us consider that you have the following list of friends:

Amigo[] agenda = {
    new Amigo("João", "20/01/1983"),
    new Amigo("Marcia", "12/08/1990"),
    new Amigo("Lucas", "02/12/1989"),
    new Amigo("Tereza", "30/08/1985")
}

You want to get the birthday list of the month 08, then call the method:

String aniversariantes = objeto.aniversariarNoMes(8);

Considering the initial implementation of the method:

public static String aniversariarNoMes(int mes) {
    String saida = "";

    if (mes < 01 || mes > 12) {
        return "Mês invalido.";
    } else {
        for (Amigo amigo : agenda) {
            if (mes == amigo.getMesNas()) {
                saida = amigo.getNome() + "\n";

                return saida;
            }

            return saida+=amigo.getNome()+"\n";
        }
    }

    return null;
}

Follow the steps that will be performed (table test):

  1. The method aniversariarNoMes is called with the value mes = 8;
  2. A string saida empty;
  3. Checks whether the month reported is valid (between 1 and 12 inclusive);
  4. The value is valid, then the else;
  5. Scroll through the list of friends defined in agenda;
    1. The value of amigo will be Amigo("João", "20/01/1983");
    2. Check whether the anniversary month of amigo is 8;
    3. False, does not run the if;
    4. Returns the value of saida, concatenated with the name of amigo;

As it was returned, the method is finalized and therefore the returned value would be João\n. Besides having returned before the hour, returned an unexpected value, since John makes birthday in the month 01. With this you already know that this return it makes no sense to stay there. Removing it, it:

public static String aniversariarNoMes(int mes) {
    String saida = "";

    if (mes < 01 || mes > 12) {
        return "Mês invalido.";
    } else {
        for (Amigo amigo : agenda) {
            if (mes == amigo.getMesNas()) {
                saida = amigo.getNome() + "\n";

                return saida;
            }
        }
    }

    return null;
}

Redoing the table test:

  1. The method aniversariarNoMes is called with the value mes = 8;
  2. A string saida empty;
  3. Checks whether the month reported is valid (between 1 and 12 inclusive);
  4. The value is valid, then the else;
  5. Scroll through the list of friends defined in agenda;
    1. The value of amigo will be Amigo("João", "20/01/1983");
    2. Check whether the anniversary month of amigo is 8;
    3. False, does not run the if;
    4. In the next iteration of for, amigo vale Amigo("Marcia", "12/08/1990");
    5. Check whether the anniversary month of amigo is 8;
    6. True, then runs the if;
    7. Updates the value of saida to the name of amigo;
    8. Returns the value of amigo;

As returned, again the method is finished. In this case, the return would be Marcia\n. Indeed this record makes birthday in month 8, but is not the only one, so we can not finalize the method at this time. This shows us that again this return is wrong. Removing it, gets:

public static String aniversariarNoMes(int mes) {
    String saida = "";

    if (mes < 01 || mes > 12) {
        return "Mês invalido.";
    } else {
        for (Amigo amigo : agenda) {
            if (mes == amigo.getMesNas()) {
                saida = amigo.getNome() + "\n";
            }
        }
    }

    return null;
}

Redoing the table test:

  1. The method aniversariarNoMes is called with the value mes = 8;
  2. A string saida empty;
  3. Checks whether the month reported is valid (between 1 and 12 inclusive);
  4. The value is valid, then the else;
  5. Scroll through the list of friends defined in agenda;
    1. The value of amigo will be Amigo("João", "20/01/1983");
    2. Check whether the anniversary month of amigo is 8;
    3. False, does not run the if;
    4. In the next iteration of for, amigo vale Amigo("Marcia", "12/08/1990");
    5. Check whether the anniversary month of amigo is 8;
    6. True, then runs the if;
    7. Updates the value of saida to the name of amigo;
    8. In the next iteration of for, amigo vale Amigo("Lucas", "02/12/1989");
    9. Check whether the anniversary month of amigo is 8;
    10. False, does not run the if;
    11. In the next iteration of for, amigo vale Amigo("Tereza", "30/08/1985");
    12. Check whether the anniversary month of amigo is 8;
    13. True, then runs the if;
    14. Updates the value of saida to the name of amigo;
  6. The list is finalized, so the for;
  7. Returns null;

We already realize that now it’s always returning null, then there must be another return in the code that returns the correct value to us. As we have seen within the for does not work, just put out of it:

public static String aniversariarNoMes(int mes) {
    String saida = "";

    if (mes < 01 || mes > 12) {
        return "Mês invalido.";
    } else {
        for (Amigo amigo : agenda) {
            if (mes == amigo.getMesNas()) {
                saida = amigo.getNome() + "\n";
            }
        }

        return saida;
    }

    return null;
}

But if you analyze item 5.14 of the last table test, you will see that the value of saida ends up valid Tereza\n. This is not what we want, but the list of names of the birthday kids. That is, within the if, we can not just make the simple assignment to the value of saida, but to concatenate to the current value. To do this:

public static String aniversariarNoMes(int mes) {
    String saida = "";

    if (mes < 01 || mes > 12) {
        return "Mês invalido.";
    } else {
        for (Amigo amigo : agenda) {
            if (mes == amigo.getMesNas()) {
                saida += amigo.getNome() + "\n";
            }
        }

        return saida;
    }

    return null;
}

This way, the code must return Marcia\nTereza\n, which is expected, or null if there are no birthday kids. I leave the table test of this version as an activity for you. Do it and see if it really matches what the code does.

0

//Method that gets a month and returns all birthday friends

public static String aniversariarNoMes(int mes) {
    String saida = "";
    //Faz a validação do mes escolhido
    if (mes < 01 || mes > 12) {
        return "Mês invalido.";
    } else {
        //Percorre o ArrayList a procura do mes informado
        for (Amigo amigo : agenda) {
            if (mes == amigo.getMesNas()) {
                saida += amigo.getNome() + "\n";

            }

        }
        return saida;
    }



}
  • This you have already posted on the question. Ah, this field is for answers. If you are updating the question with the Friend class and how you call this method, edit the question.

  • This code seems to make sense now. Are you posting it as a solution? If so, please describe in words what was wrong and what you did to resolve it. If it is just extra information about the question, do not use this area, but do the editing directly on the question. Reinforce the tip to do the [tour] to understand how the community works.

  • I edited the code.

  • That code worked?

  • Yes. I made a comment about this new code, but I don’t know where it went.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.