I have this code in Java:
for(int i=0;i<num;i++){
if(i!=0){
r = r + "+";
}
if(ProblemaVagones.listaVagones.get(i).isElectricidad()){
r = r + "x" + i;
r.substring(0, r.length()-1);
}
}
My output from this piece of code is this:
x0+x1+x2+<=2;
Which is wrong because at the end it has a "+" that is left over, I have tried to make a substring from 0 to the penultimate element of the string to remove it, from what you see it does not work. Does anyone see the error?
You have to retrieve the result of the substring in a String (which can be the same on which you execute the operation):
Inside the loop it checks if it
i
is in a different position than the last one. Your code would look like this: