00 Votes

Java: Convert String to Integer

Question by Guest | 2016-10-01 at 09:19

Is there any possibility to change or convert a string into an integer value in Java?

I have some user inputs that are naturally only available as string. Although the string only contains or consists of digits and numbers, I cannot calculate with it. I know functions like inttostr from other programming languages, but such a function seems not to be available in Java. What can I do? 

ReplyPositiveNegative
0Best Answer0 Votes

You can use the function Integer.parseInt() for that purpose. This function returns an integer value for a given string. 

For example:

int i = Integer.parseInt("123");

Or:

string s = "123";
int i = Integer.parseInt(s);

However, if you want to convert user input, you should make sure that the input respectively the string really only consists of digits. For that, you can check whether the string only contains digits before the conversion or you can return a default value in doubt.
2016-10-01 at 16:11

ReplyPositive Negative
Reply

Related Topics

Android Getting Sound Levels

Open Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.