Saturday, 18 July 2015

How to convert String to Integer.

Most of the interviewer ask to this question. Believe me, it is so simple problem to solve and required basic knowledge. Lets see how we can do it java.

public class ConvertStringNumberToInteger{
           public static void main(String args[]) throws NumberFormatException{
                       String input="12345";
                        int output=0;
                       for(int j=0;j<input.length();j++){
                             int temp=input.charAt(j)-'0';
                             if(0<=temp && temp<=9){
                                    throw new NumberFormatException("String should contain only number ");
                       System.out.println("output : "+output)

No comments:

Post a Comment