-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.

0

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

It helped me, thanks.
The above answer will cause an exception if the string cannot be parsed to an int. See stackoverflow.com/questions/5585779/… for a more in depth answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.