String stringFirstForRandom = Integer.toString(lastForRandom); int firstForRandom = stringFirstForRandom.lastIndexOf("0"); StringBuffer sb = new StringBuffer(stringFirstForRandom); sb.deleteCharAt(firstForRandom); stringFirstForRandom = sb.toString(); firstForRandom = Integer.parseInt(stringFirstForRandom); return (int)(firstForRandom + Math.random() * lastForRandom); 

This is my decision, but it turned out clumsily. Is there a way to make everything more careful? It is necessary to remove either the last digit in the int variable or the last character in the string.

  • describe the problem, not the solution. since you obviously have some more difficult task than simply removing 1 character. - Mikhail Vaysman
  • one
    For an integer it is enough to divide by 10 - Alexey Shimansky
  • @ Alexey Shimansky I can’t understand how I didn’t guess. - Anton Sorokin

1 answer 1

If the initial value is an integer:

 int someNumber = 1234; (int) someNumber / 10 // или Integer.valueOf(someNumber / 10) 

If string:

 String someNumber = "1234"; someNumber.substring(0, someNumber.length() - 1)