Conversion of String to Int using Java with Exception class

Hi Everyone, this post will help you in understanding how to convert a String to an Int using Java. The string is given as an input from command line. If invalid string is given, an exception will be thrown using the exception class defined.

The logic for converting from String to Int is as follows:
  • If the string is null, invalid exception is thrown.
  • Otherwise check if character of the String. If the first character is negative, a flag is set to indicate that the number is  negative.
  • Convert the character to corresponding number.
  • For every other number multiply the number with 10 to the power of order of number (starting from 0)   and add it to the previous number. For eg: for 23, add (3 * 10 ^ 0) and (2 * 10 ^ 1).
  • If an invalid number is passed, the exception is passed. For eg: in the case "12h5", an exception will be called for 'h'.
  • Return the number when all characters of string are processed.
The program to convert String to Int is as follows: -

 package com.program;  
 import java.util.Scanner;  
 public class StringToInt {  
      public static void main(String[] args) throws InvalidNumberException{  
           Scanner scan = new Scanner(System.in);  
           System.out.println(" Enter the String : ");  
           String str = scan.next();  
           int convertedInt = StrToInt(str);  
           System.out.println(" The integer representation of "+ str +" is " + convertedInt);  
      }  
      public static int StrToInt(String str) throws InvalidNumberException  
      {  
           if(str==null)  
           {  
                throw new InvalidNumberException("The string is invalid");  
           }  
           int returnNum = 0, count = 0;  
           boolean isNegative = false;  
           int length = str.length();  
           if(str.charAt(0) == '-')  
           {  
                isNegative = true;  
                count = 1;  
           }  
           while(count < length)  
           {  
                returnNum = returnNum * 10;  
                int addNum = str.charAt(count) - '0';  
                count++;  
                if(!((addNum >= 0) && (addNum <= 9)))  
                          {  
                          throw new InvalidNumberException("The string is invalid");  
                          }  
                returnNum = returnNum + addNum;  
           }  
           if(isNegative)  
           {  
                returnNum = returnNum * -1;  
           }  
           return returnNum;  
      }  
 }  

The program for exception class is as follows:

 package com.program;  
 public class InvalidNumberException extends Exception{  
      public InvalidNumberException()  
      {  
           System.out.println(" The string is invalid");  
      }  
      public InvalidNumberException (String message)  
      {  
                     super(message);  
      }  
      public InvalidNumberException (Throwable cause)  
      {  
           super(cause);  
      }  
      public InvalidNumberException (String message, Throwable cause)  
      {  
           super (message, cause);  
      }  
 }  

The output of the program for valid and invalid case is as follows:

1) Valid case

  Enter the String :   
 1242  
  The integer representation of 1242 is 1242  

2) Invalid case

  Enter the String :   
 cool  
 Exception in thread "main" com.program.InvalidNumberException: The string is invalid  
      at com.egen.training.StringToInt.StrToInt(StringToInt.java:44)  
      at com.egen.training.StringToInt.main(StringToInt.java:13)  

I hope everyone understood this program well. Please comment below if you have any doubts / suggestions. All comments will be responded soon.

Comments

Popular posts from this blog

Difference between "diff" and "sdiff" commands in Unix

Anonymous classes in C++