TheDeveloperBlog.com


Java ParseInt: Convert String to Int

Integer.parseInt. A String cannot be used like an int. It cannot be incremented. It contains chars, textual data. With parseInt we convert a string to an int.


This method, though useful, can lead to complex problems. It throws a NumberFormatException on invalid data. Many exceptions will lead to slower program performance.


ParseInt, parseUnsignedInt. This program uses the parseInt method and its friend parseUnsignedInt. For the string "1000," we get the number 1000.

Tip: We always want to assign an int variable to the result of the parseInt method. This is the parsing result.

Unsigned: The method parseUnsignedInt does nearly the same thing as parseInt, but does not allow negative numbers in the argument string.

Based on:

Java 7

Java program that uses parseInt, parseUnsignedInt

public class Program {

    public static void main(String[] args) {

	String value = "1000";
	// Parse with parseInt: the string can be negative.
	int result = Integer.parseInt(value);
	System.out.println(result);
	System.out.println(result + 1);

	// Parse with parseUnsignedInt: minus sign not allowed.
	int result2 = Integer.parseUnsignedInt(value);
	System.out.println(result2);
	System.out.println(result2 + 2);
    }
}

Output

1000
1001
1000
1002

NumberFormatException. This dreaded error is caused by trying to parse an invalid number string. For example the string "turnip" cannot be converted into an integer.

Note: To avoid NumberFormatException, we can develop a method that tests for validity before calling parseInt.

Java program that causes NumberFormatException

public class Program {

    public static void main(String[] args) {

	String invalid = "turnip";
	// This causes a NumberFormatException.
	int result = Integer.parseInt(invalid);
    }
}

Output

Exception in thread "main" java.lang.NumberFormatException:
    For input string: "turnip"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)....

ParseUnsignedInt error. The unsigned method here cannot handle strings that have a leading minus sign. A leading plus is accepted. The result is always a positive number.

Java program that uses parseUnsignedInt, causes error

public class Program {

    public static void main(String[] args) {

	String test = "-100";
	// ParseInt handles signed integers.
	int value = Integer.parseInt(test);
	System.out.println(value == -100);

	// ParseUnsignedInt throws an Exception.
	value = Integer.parseUnsignedInt(test);
    }
}

Output

true
Exception in thread "main" java.lang.NumberFormatException:
    Illegal leading minus sign on unsigned string -100.
    at java.lang.Integer.parseUnsignedInt....

Try, catch. We can handle errors caused by Integer.parseInt with a try and catch construct. This is slow if an error occurs, but it works. We can assign a default value in the catch block.

Exceptions
Java program that uses try, catch, parseInt

public class Program {

    public static void main(String[] args) {

	String invalid = "turnip";
	int result = -100; // A default result.
	try {
	    // This throws an exception because the string is invalid.
	    result = Integer.parseInt(invalid);
	} catch (NumberFormatException n) {
	    // ... Do nothing.
	}
	System.out.println(result); // Prints default result.
    }
}

Output

-100

Test for valid strings. We introduce a method that scans strings for digit characters. With isValidNumber, we see if a number will be parsed without an exception before calling parseInt.

IsDigit: The Character.isDigit method returns true for valid digits, and false otherwise.

Note: This program does not cause any exceptions, even though some of the strings are not valid numeric data.

Java program that checks for valid number strings

public class Program {

    static boolean isValidNumber(String value) {
	// Loop over all characters in the String.
	// ... If isDigit is false, this method too returns false.
	for (int i = 0; i < value.length(); i++) {
	    if (!Character.isDigit(value.charAt(i))) {
		return false;
	    }
	}
	return true;
    }

    public static void main(String[] args) {

	String[] values = { "cat", "100", "200", "dog" };
	for (String value : values) {
	    // If this is a valid number, parse it.
	    if (isValidNumber(value)) {
		int number = Integer.parseInt(value);
		System.out.println(number);
	    }
	}
    }
}

Output

100
200

Performance. Exceptions are slow. Here we reuse the isValidNumber method. We call this method before using parseInt in the first version. In the second, we handle exceptions from parseInt.

Result: The two versions have similar functionality, and with the data array, do the same thing.

But: Checking for valid strings (with isValidNumber or a similar method) is many times faster when invalid data is encountered.

Java program that times invalid int string parsing

public class Program {

    static boolean isValidNumber(String value) {
	// Return true if all characters are digits.
	for (int i = 0; i < value.length(); i++) {
	    if (!Character.isDigit(value.charAt(i))) {
		return false;
	    }
	}
	return true;
    }

    public static void main(String[] args) {

	String[] values = { "cat", "100", "200", "dog" };

	long t1 = System.currentTimeMillis();

	// Version 1: call isValidNumber before parseInt.
	for (int i = 0; i < 1000000; i++) {

	    int sum = 0;
	    for (String v : values) {
		if (isValidNumber(v)) {
		    sum += Integer.parseInt(v);
		}
	    }
	}

	long t2 = System.currentTimeMillis();

	// Version 2: handle Exception on invalid numbers.
	for (int i = 0; i < 1000000; i++) {

	    int sum = 0;
	    for (String v : values) {
		try {
		    sum += Integer.parseInt(v);
		} catch (Exception ex) {
		    // ... Do nothing.
		}
	    }
	}

	long t3 = System.currentTimeMillis();

	// ... Times.
	System.out.println(t2 - t1);
	System.out.println(t3 - t2);
    }
}

Results

  72 ms:    isValidNumber, Integer.parseInt
1686 ms:    try, Integer.parseInt, catch

ParseDouble. A double can be parsed in the same way as an int. But we must use parseDouble on the Double class. This will handle numbers with a decimal place.

Java that uses parseDouble

public class Program {
    public static void main(String[] args) {

	String data = "12.01";

	// Parse the double in the String.
	double value = Double.parseDouble(data);
	if (value == 12.01) {
	    System.out.println("String converted to Double!");
	}
    }
}

Output

String converted to Double!

Fast int parsing. Let us consider a custom, optimized parsing algorithm. ParseIntFast here progresses from left to right in a string. It converts each char to an integer by subtracting 48.

And: To handle digits, it multiplies by 10—so the digit to the left of the current one is ten times greater in the result int.

Result: The parseIntFast method requires less than half the time of Integer.parseInt. It may also avoid exceptions.

Note: For validation of numbers, we could add logic to test each char in parseIntFast. We could also add support for negative numbers.

Java that implements fast int parsing

public class Program {

    public static int parseIntFast(String value) {
	// Loop over the string, adjusting ASCII value to integer value.
	// ... Multiply previous result by 10 as we progress.
	int result = 0;
	for (int i = 0; i < value.length(); i++) {
	    result = 10 * result + (value.charAt(i) - 48);
	}
	return result;
    }

    public static void main(String[] args) {

	String test = "1234";
	System.out.println(parseIntFast(test));

	long t1 = System.currentTimeMillis();

	// Version 1: use optimized parse method.
	for (int i = 0; i < 10000000; i++) {
	    int v = parseIntFast(test);
	    if (v != 1234) {
		return;
	    }
	}

	long t2 = System.currentTimeMillis();

	// Version 2: use built-in parse method.
	for (int i = 0; i < 10000000; i++) {
	    int v = Integer.parseInt(test);
	    if (v != 1234) {
		return;
	    }
	}

	long t3 = System.currentTimeMillis();

	// ... Times.
	System.out.println(t2 - t1);
	System.out.println(t3 - t2);
    }
}

Results

1234
 74 ms    parseIntFast
168 ms    Integer.parseInt

Console input. We can use Integer.parseInt to parse the input read from a terminal (console) window. The try and catch blocks are required to avoid failures.

Console: Interactive

With parseInt, and custom helper methods, we handle numeric data in strings. We can read in integers from text files and convert them into usable numbers. This step is powerful and common.