Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
Printing both name and salary

So I am reading from a file(Consist of name and salary) and inserting into a tree. After that, I am doing a search method. It's working and printing true if the node exists. How do I print the actual name and salary just by searching by salary? Here is my search method:

    public boolean find(int salary) {
    Add current = root;
    while (current != null) {
        if (current.getSalary() == salary) {
            return true;
        } else if (current.getSalary() > salary) {
            current = current.getLeft();

        } else {
            current = current.getRight();
        }

     }
   return false;
}

And Here is my main method:

public static void main(String[] args) throws IOException {

    BinaryTree bt = new BinaryTree();

    bt.readfile();

    bt.print(bt.root);

    System.out.println();

    System.out.println("Find method: return true if a node exits ,else return flase");

    System.out.println(bt.find(8900));`
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

If find() returns a node, write a toString() method for the node such that the name is expressed.

Example:

@Override
public String toString(){

    return this.name + " " + this.salary;

}

Why this works:

When the System.out.println() calls on your node it will automatically call on the memory address. If you override this call with your custom method you can define what variables are printed out.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda