DEV Community

Cover image for Convert String to Integer
Suchitra
Suchitra

Posted on

Convert String to Integer

Today i stacked in a problem then i knew this and so i want to share with you!

It is a very simple problem but i think most of the people still doesn't know about it because we generally use some predefined functions for it such as
Integer.parseInt() and Integer.ValueOf()
Here is the user-defined function for converting string to integer.
For example:Input- String s="2345"
Output- int a=2345

import java.util.*;
class Solution
{
public satic void main(String args[])

{
String s="2345";
int d=Int_val(s);
System.out.println(d);
}
public static int Int_val(String s)
{
int result=0;
for(char c:s.toCharArray())
{

result=result*10+c-'0';
}
return result;
}
}

HappyCoding❤️
I hope you like

Top comments (0)