convert string to int without parseint

Convert string to integer without using build in api Integer.parseInt()

   Using this concept we can convert String to Number, String to Float, String to Double, String to short and long
                                                       
ASCII code of decimal number start form 48

  • public class ConvertStringToInt {

  • public static void main(String[] args) {
  • String str="-14545";
  • int total=0;
  • boolean m=false;
  • for(int i=0;i<str.length();i++){
  • if(str.charAt(i)=='-'){
  • m=true;
  • continue;
  • }
  • if(str.charAt(i)=='+'){
  • continue;
  • }
  • total=(total*10)+(str.charAt(i)-48);
  • }
  • if(m){
  • total=0-total;
  • }
  • System.out.println(total);


  • }

  • }



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation