How to Convert String to int in Java

Connect with

Oracle Java
There are various way to convert from string to int in java. It depends where you have to applied this. if any interview will ask then you can ask him/her , library can be used or not. if he/she says yes, then reply by using Integer class to convert but if he wanted to check you logic then you have to chose plain way to convert from string to int. Please ask precondition to Interviewer before replying anything .

By using Integer class

If you are using in your project then better to use following approach i.e. Integer class to convert from string to int value.

int i = Integer.parseInt("234");

//OR
int  aIntVal = Integer.valueOf("234").intValue();

Here , in the above code snipet:
Integer.parseInt() method return an int value .
Integer.valueOf() method returns Integer wrapper object from which you can get int value by calling intValue() method.

Method to convert by using For Loop


/**
 * This java method to convert from string to number.
 * String can be negative of positive.
 * 
 * @param numStr
 * @return
 */
public static int stringToNumber(String numStr) {

	char ch[] = numStr.toCharArray();
	int sum = 0;
	boolean isFlag = false;
	int i = 0;
	
	if (ch[0] == '-') {
		i = 1;
		isFlag = true;
	}
	
	for (; i < ch.length; i++ ) {
		sum = (sum * 10) + (ch[i] - '0');
	}
	
	// get ascii value for zero
	/*int zeroAscii = (int) '0';
	for (; i < ch.length; i++ ) {
		int tmpAscii = (int) ch[i];
		sum = (sum * 10) + (tmpAscii - zeroAscii);
	}*/
	
	if (isFlag) {
		sum = -sum;
	}
	return sum;
}

Using while Loop

/**
 * This method is used to convert from string to int.
 * 
 * @param inutStr
 * @return
 */
public static int strToInt(String inutStr) {
	int i = 0;
	int num = 0;
	boolean isFlag = false;

	// check for negative sign if it's there, set the isNeg=true and
	// initialize i to 1
	if (inutStr.charAt(0) == '-') {
		isFlag = true;
		i = 1;
	}

	while (i < inutStr.length()) {
		// Minus the ASCII code of '0' i.e. 48 to get the value of the
		// charAt(i++).
		num = num * 10 + (inutStr.charAt(i++) - '0');
	}

	if (isFlag) {
		num = -num;
	}
	return num;
}

By Using Apache Commons Library

There is an alternate solution is to use Apache Commons' NumberUtils but I don't use this just for sake of conversion.

int num = NumberUtils.toInt("1234");

Complete Example

package com.core;

public class StringToNumber {

	public static void main(String[] args) {

		System.out.println("\"-3256\" == " + stringToNumber("-3256"));
		System.out.println("\"3256\" == " + stringToNumber("3256"));
		System.out.println("\"-785\" == " + strToInt("-785"));
	}

	/**
	 * This java method to convert from string to number.
	 * 
	 * @param numStr
	 * @return
	 */
	public static int stringToNumber(String numStr) {

		char ch[] = numStr.toCharArray();
		int sum = 0;
		boolean isFlag = false;
		int i = 0;
		
		if (ch[0] == '-') {
			i = 1;
			isFlag = true;
		}
		
		for (; i < ch.length; i++ ) {
			sum = (sum * 10) + (ch[i] - '0');
		}
		
		// get ascii value for zero
		/*int zeroAscii = (int) '0';
		for (; i < ch.length; i++ ) {
			int tmpAscii = (int) ch[i];
			sum = (sum * 10) + (tmpAscii - zeroAscii);
		}*/
		
		if (isFlag) {
			sum = -sum;
		}

		return sum;
	}

	/**
	 * This method is used to convert from string to int.
	 * 
	 * @param inutStr
	 * @return
	 */
	public static int strToInt(String inutStr) {
		int i = 0;
		int num = 0;
		boolean isNeg = false;

		// check for negative sign if it's there, set the isNeg=true and
		// initialize i to 1
		if (inutStr.charAt(0) == '-') {
		  isNeg = true;
		  i = 1;
		}

		while (i < inutStr.length()) {
		  // Minus the ASCII code of '0' i.e. 48 to get the value of the charAt(i++).
                  num = num * 10 + (inutStr.charAt(i++) - '0');
		}

		if (isNeg) {
			num = -num;
		}
		return num;
	}
}

The Apache utility is nice because if the string is an invalid number format then 0 is always returned. Hence saving you the try catch block.

Apache NumberUtils API Version 3.4


Connect with

Leave a Reply

Your email address will not be published. Required fields are marked *