How to cast in Java?

Asked

Viewed 404 times

0

This model is working, look at the figure!

inserir a descrição da imagem aqui

You may notice that you are concatenating two String variables as you can see in the figure below;

inserir a descrição da imagem aqui

My problem now is to be able to concatenate an Integer variable with a String variable, because the eclipse does not allow it, as you can see below;

inserir a descrição da imagem aqui

I’d have to make one Cast, but I am having difficulty to do for lack of experience, I need help in this direction!

That is the code;

/*lista de NaoConformidade*/
@Autorizacao(values = { AutorizacaoSistema.M_ADM })
public List<TipoInconsistenciaEntity> getListNaoConformidade() {
    AbstractCriteriaSearch<TipoInconsistenciaEntity> filtro =
            new AbstractCriteriaSearch<TipoInconsistenciaEntity>(new TipoInconsistenciaEntity());
    filtro.addOrder("codigo", ASC);
    List<TipoInconsistenciaEntity> lista = tipoInconsistenciaService.listarEntidades(filtro);
    List<TipoInconsistenciaEntity> listaRetorno = new ArrayList<TipoInconsistenciaEntity>();

    for (TipoInconsistenciaEntity pj : lista) {
        if(pj.getCodigo() > 5000) {
            TipoInconsistenciaEntity p = new TipoInconsistenciaEntity();
            p.setDescricao(pj.getDescricao());
            p.setCodigo(pj.getCodigo() + " - ["+ pj.getDescricao() + "]");

            listaRetorno.add(p);
        }
    }
    return listaRetorno;
}
  • Good morning, man.. you can only receive an Integer field there, there is no way to cast because it is a description and contains non-numeric characters. Either you change your object and consequently your getters and setters or write the code (which makes more sense).

  • there is no way to change the object because they are being used elsewhere in the project, if I change the object will be considerably disadvantageous.

  • I imagine so, but out of curiosity, why do you want to put the code (string) in an integer? Why not use the registration ID directly?

  • I found the answer, and I put the solution, I had the help of a co-worker.

  • I still didn’t understand the meaning of it, but if you did it’s all that matters! :)

  • If shown the registration ID along with the description is a customer requirement!

Show 1 more comment

1 answer

0

I created a variable in the entity receiving Getts and Setts

inserir a descrição da imagem aqui

Then I made this little change in the method;

inserir a descrição da imagem aqui

And it worked!

inserir a descrição da imagem aqui

Browser other questions tagged

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