Repeating code at listing time

Asked

Viewed 79 times

3

I’m having a repeat problem at listing time, in my view it repeats R01 and R01, twice the same code and only then it correctly follows R02, RO3 and so on. The correct would be R01, R02, R03 and so goes. 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;
}
  • only one comment, arrays start at 0 (zero) but Voce puts the variable i starting at 1. Once inside the function, Voce has if(numItem == 0) but that will never happen, because it is passing the first as 1... change to int i = 0;

  • In this case it is using the variable "i" to set the numRequisite, so it starts from 1. R01, R02, R03, etc...

2 answers

1


I managed to solve this way, see an example here too: https://ideone.com/1ucEGY

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

    return strNumItem;

0

Replace the following code snippet:

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

for

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

requisitoFuncional.setStrNumRequisito(aux);

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

  • It did not work, I did this procedure but it did not work. Continue listing but repeating the first two fields.

  • @Fredericoqueiroz, there is another problem there, certainly not in the definition of the code then. If you print the aux variable right after creating it, the code repeats?

  • Yes the first two RO yes. RO1, RO1 RO2.

  • @Fredericoqueiroz, if you do this: "String aux = "R00". substring(0,"R00".length()-String.valueOf(i).length())+String.valueOf(i); System.out.println(aux);" the code repeats?

  • Yeah, keep going, I’ll have to take a slow look.

  • Honestly, I don’t see how that happens. For this to occur its variable "i" should decrease at some point or else, for some reason, not be incremented. Exception handling is not, as if an exception is thrown before i is incremented, the execution of the for is stopped.

  • Thanks for your help, buddy.

Show 2 more comments

Browser other questions tagged

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