/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String a = "2147483647";
		System.out.println("Integer : " + atoi(a));
	}
	
	public static int atoi(String a) throws Exception
	{
		String sane = checkSanity(a);
		if( sane == null )
		{
			throw new Exception("Not compatible");
		}
		
		if( sane.length() > 10 )
		{
			throw new Exception("Overflow");
		}
		
		if( sane.length() <= 0 )
		{
			throw new Exception("Underflow");
		}
		
		int convertedInt = 0;
		int exponent = 0;
		for( int i = sane.length() - 1; i >= 0 ; i-- )
		{
			int newDigit = Character.getNumericValue( sane.charAt(i) );
			
			int newValueToAdd = (int) ( Math.pow(10, exponent) * newDigit );
			
			if( convertedInt <= Integer.MAX_VALUE - newValueToAdd )
			{
				convertedInt += newValueToAdd;	
			}
			else
			{
				throw new Exception("Overflow");
			}
			
			exponent++;
		}
		
		return convertedInt;
	}
	
	public static String checkSanity(String a)
	{
		// could be null
		if( a == null )
		{
			return null;
		}
		
		// Trim leading and trailing spaces.
		String b = a.trim();
		
		// Should it have + or - symbol, consider.
		String c;
		if( b.charAt(0) == '+' || b.charAt(0) == '-' )
		{
			c = b.substring(1);
		}
		else
		{
			c = b.substring(0);
		}
		
		// could have special characters and non numbers.
		for( int i = 0; i < c.length(); i++ )
		{
			if( ! Character.isDigit(c.charAt(i)) )
			{
				return null;
			}
		}
		
		return c;
	}
}