Problem to use a "System.out" on an object

Asked

Viewed 116 times

3

I’m trying to give a System.out.println on an object I have (even with the toString() in class) I’m getting:

Predio [name=LS, aptos=[[Lmodel.Apartamento;@7ba4f24f, [Lmodel.Apartment;@3b9a45b3, [Lmodel.Apartment;@7699a589, [Lmodel.Apartment;@58372a00, [Lmodel.Apartment;@4dd8dc3]]

Mine models:

Building:

package model;

import java.util.Arrays;

public class Predio {
    private String nome;

    private Apartamento[][] aptos;

    public Predio(int qtdAndares, int aptosPorAndar, String nome) {
        this.aptos = new Apartamento[qtdAndares][aptosPorAndar];
        this.nome = nome;
    }

    public void adicionarApto(int andar, Apartamento apto) {
        if (andar >= this.aptos.length) {
            throw new IllegalArgumentException("O prédio só tem " + this.aptos.length + " andares");
        }
        // encontra a primeira posição não preenchida
        int i = 0;
        while (i < this.aptos[andar].length && this.aptos[andar][i] != null)
            i++;
        if (i >= this.aptos[andar].length) {
            throw new IllegalArgumentException("Andar " + andar + " já está com todos os apartamentos cadastrados");
        }
        this.aptos[andar][i] = apto;
    }

    public String getNome() {
        return nome;
    }
    public void setNome(String nome) {
        this.nome = nome;
    }

    @Override
    public String toString() {
        return "Predio [nome=" + nome + ", aptos=" + Arrays.toString(aptos) + "]";
    }



}

Apartment:

package model;

public class Apartamento {
    private Integer qtdQuarto, qtdBanheiro, qtdCozinha, qtdSala, numeroApt;



    public Apartamento(Integer numeroApt) {
        this.numeroApt = numeroApt;
    }

    public Integer getNumeroApt() {
        return numeroApt;
    }

    public void setNumeroApt(Integer numeroApt) {
        this.numeroApt = numeroApt;
    }

    public Integer getQtdQuarto() {
        return qtdQuarto;
    }

    public void setQtdQuarto(Integer qtdQuarto) {
        this.qtdQuarto = qtdQuarto;
    }

    public Integer getQtdBanheiro() {
        return qtdBanheiro;
    }

    public void setQtdBanheiro(Integer qtdBanheiro) {
        this.qtdBanheiro = qtdBanheiro;
    }

    public Integer getQtdCozinha() {
        return qtdCozinha;
    }

    public void setQtdCozinha(Integer qtdCozinha) {
        this.qtdCozinha = qtdCozinha;
    }

    public Integer getQtdSala() {
        return qtdSala;
    }

    public void setQtdSala(Integer qtdSala) {
        this.qtdSala = qtdSala;
    }

    @Override
    public String toString() {
        return "Apartamento [qtdQuarto=" + qtdQuarto + ", qtdBanheiro=" + qtdBanheiro + ", qtdCozinha=" + qtdCozinha
                + ", qtdSala=" + qtdSala + "]";
    }


}

my main:

package main;

import java.util.Scanner;

import model.Apartamento;
import model.Predio;

public class app {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        System.out.println("Digite o nome do Predio: ");
        String nomePredio = scanner.next();

        System.out.println("Digite a quantidade de Andares do Predio: ");
        int qtdAndares = scanner.nextInt();

        System.out.println("Digite a quantidade de Apartamentos por andar: ");
        int aptosPorAndar = scanner.nextInt();

        System.out.println("Digite o prefixo de numeração dos Apartamentos: ");
        int prefApt = scanner.nextInt();

        Predio predio = new Predio(qtdAndares, aptosPorAndar,nomePredio);


        for (int andar = 0; andar < qtdAndares; andar++) {
            for (int apto = 0; apto < aptosPorAndar; apto++) {
                int numero = andar * prefApt + apto;
                // leia todos os dados que um apartamento precisa e passe todos para o construtor
                predio.adicionarApto(andar, new Apartamento(numero));
            }
        }

        System.out.println(predio);
    }

}

1 answer

4


You’re calling this one toString() (who accepts a Object) because it does not have a method to print the textual representation of debugging a array of Apartamento, has only one that prints a array of Object that prints exactly what is appearing, after all a Object is something generic. If you want something other than this you have to write a code that goes through the array and print the way you want, even because the toString() does not serve what you think it serves.

The use of toString() is wrong, but as you’ve probably learned wrong somewhere and people tend to keep doing wrong what you’ve learned in the first place you probably don’t want to solve this. I hope the answer there helps understand that the whole concept is wrong.

The solution is to have a different method that assembles data for you manually by making a loop and picking up what you want from array and the elements contained in it. Forget this idea of having the object printed and it will leave the way you want.

Even don’t make the conceptual error of the object providing something with layout specific, the object should only provide the data, the layout should be given by something external, because for each situation will be a layout different.

There is an automatic solution with reflection, but it is slow and easy to do wrong, in general it is wrong use to save typing and will not solve your problem in a specific way that is what you want.

  • Thank you I am trying to modify the code with your hints and removed toString, but with that I entered another problem, I think I will have to ask a new question.

  • 1

    You can do it, we’re there for it.

Browser other questions tagged

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