Repeating code at the time of listing

3

I have a repetition problem at the time of listing, in my view it repeats R01 and R01, twice the same code and only after it correctly follows R02, RO3 and so on. The correct one would be R01, R02, R03 and so on. Thanks.

public void reorganizarNumerosItens() {
    List<Listitem> itens = this.getItems();

    if (itens != null) { 
        int i = 1;
        for (Listitem itemAtual : itens) {
            ListitemRequisitoFuncional itemReq = (ListitemRequisitoFuncional) itemAtual;
            RequisitoFuncional requisitoFuncional = itemReq.getReqFuncional();
            requisitoFuncional.setStrNumRequisito(this.getStrNumItemAtual(i));
            i++;
            itemReq.recarregaObjeto();

            try {
                this.bo.salvar(requisitoFuncional);
            } catch (NegocioException e) {
                e.printStackTrace();
            }
        }
    }
}

public String getStrNumItemAtual(Integer numItem) {
     if (numItem == 0) {
        numItem +=1; 
    }
    String strNumItem = numItem.toString();
    strNumItem = strNumItem.length() < 2 ? "R0" + strNumItem : "R" + numItem;

    return strNumItem;
}
    
asked by anonymous 10.08.2015 / 20:34

2 answers

1

I was able to solve this, see an example here too: link

    numItem +=1;
    String strNumItem = numItem.toString();
    strNumItem = strNumItem.format(strNumItem, numItem) != null ? "RF0" + strNumItem : "RF" + numItem;

    return strNumItem;
    
24.08.2015 / 20:28
0

Replace the following code snippet:

requisitoFuncional.setStrNumRequisito(this.getStrNumItemAtual(i));

by

String aux = "R00".substring(0,"R00".length()-String.valueOf(i).length())+String.valueOf(i);

requisitoFuncional.setStrNumRequisito(aux);

This will eliminate the function getStrNumItemAtual . Probably her is her problem.

    
10.08.2015 / 20:53