DEV Community

Cover image for Convert String to Integer
Suchitra
Suchitra

Posted on

4

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)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay