When you click on a button, a number is generated and recorded in a textView. When the number becomes too large, let's say 2,000,000,000, he puts a minus sign in front of him and the number is constantly subtracted. Those. now also generates a number, but does not add, but subtract. What is the problem?

Button code

case R.id.s70000x100000: watchCount = watchCount + r.nextInt(20000000) + 30000000; watchUsers.setText("Просмотров на канале - " + watchCount); dialogContract.cancel(); break; 

Here it is shown how it changes the sign for a minus and simply subtracts the large accumulated number and the generated number.

enter image description here

  • 2
    try using long - Silento
  • one
    most likely this is due to overflow - Xenia

1 answer 1

The maximum number in INT 2147483647, you have an overflow and because of this you get such a number. Use the long and nextLong method.

  • one
    There is a long type and an int type, there is no long int type in Java - pavlofff 4:47 pm
  • I apologize, made a little mistake, corrected) Thank you - Firepro
  • BigInteger still exists - Yuriy SPb