-1

I have tried to convert my String to an int but my code keeps crashing.

The code is as follows:

String age = getAge();
CalculateDaysSinceBirth((int) age);

But I get a NumberFormatException.

Zabuzard
  • 25,064
  • 8
  • 58
  • 82

1 Answers1

2

Try using Integer.valueOf(age) insted of (int) age

KIRCA
  • 104
  • 7
  • It helped me, thanks. – DzoonyVersace Feb 11 '21 at 22:47
  • 1
    The above answer will cause an exception if the string cannot be parsed to an int. See https://stackoverflow.com/questions/5585779/how-do-i-convert-a-string-to-an-int-in-java for a more in depth answer. – RStevoUK Feb 11 '21 at 22:48