Yes, but: Wrap it in a thin method (and eliminate the redundant else), or use an existing implementation, like Commons Lang's NumberUtils.toInt(str, defaultValue):

NumberUtils.toInt(myString, 0);

This method handles null values and conversion failures.

Writing the same thing on your own is straight-forward:

  • Check for null, and/or...
  • ...Wrap the NumberFormatExtension exception
Answer from Dave Newton on Stack Overflow
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-8318646
Integer#parseInt("") throws empty NumberFormatException ...
When you parse an empty string with Integer#parseInt(""), it thros an NumberFormatException with an empty message. Before the change in · JDK-8317515 it was correctly using NumberFormatException#forInputString("",..) which creates a correct message like: java.lang.NumberFormatException: For ...
🌐
How to do in Java
howtodoinjava.com › home › string › convert string to int in java
Convert String to int in Java (with Examples) - HowToDoInJava
January 10, 2023 - In the following Java program, we are parsing the string “1001” using all three above-mentioned methods and verifying that output. Assertions.assertEquals(1001, Integer.parseInt("1001")); Assertions.assertEquals(513, Integer.parseInt("1001", 8)); Assertions.assertEquals(1001, Integer.parseInt("amount=1001", 7, 11, 10)); All three methods throw NumberFormatException if: the argument string is null. the argument string length is zero i.e. empty string.
Top answer
1 of 2
2

This line is throwing the NumberFormatException:

Integer.parseInt(houredit.getText().toString());

Because houredit.getText().toString() return an empty string and you can't parse empty String into Integer.

Before you parse check that the houredit.getText().toString() don't return with empty string.

2 of 2
0

I know this is an old question, but the current answers didn't seem to have multiple answers or examples of how to fix it. The reason why you are getting this is because it is getting a NumberFormatException and because the string is empty it can't parse it and crashes, so there are two ways you can fix it.

The first way would be to check to see if the string is empty and if not then follow through with the parsing of the data.

if (!houredit.getText().toString().trim().equals("")){
 int hour = Integer.parseInt(houredit.getText().toString());
 int minute = Integer.parseInt(minuteedit.getText().toString());
 Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
 intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
 intent.putExtra(AlarmClock.EXTRA_MINUTES, minute);

 if (hour <= 24 && minute <= 60) {
   startActivity(intent);
 }
}

The second way would be with a try/catch, which tells the app to try something and if it can't do it throw an error.

try{
 int hour = Integer.parseInt(houredit.getText().toString());
 int minute = Integer.parseInt(minuteedit.getText().toString());
 Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM);
 intent.putExtra(AlarmClock.EXTRA_HOUR, hour);
 intent.putExtra(AlarmClock.EXTRA_MINUTES, minute);

 if (hour <= 24 && minute <= 60) {
   startActivity(intent);
 }
}catch(Exception ignored){} // <- capture exception if you need, else you can ignore it
🌐
Global Tech Council
globaltechcouncil.org › home › what is parseint in java?
What is parseInt in Java? - Global Tech Council
August 22, 2025 - So, if the string is ” 123 ” ... Invalid Strings: If the string does not contain a valid integer (like “abc”, “123abc”, or just an empty string “”), the function will not work properly....
🌐
Java67
java67.com › 2016 › 10 › 10-reasons-of-javalangnumberformatexception-in-java-solution.html
10 Reasons of java.lang.NumberFormatException in Java - Solution | Java67
Another common reason of java.lang.NumberFormatException is an empty String value. Many developers think empty String is OK and parseInt() will return zero or something, but that's not true.
Find elsewhere
🌐
Blogger
javahungry.blogspot.com › 2020 › 05 › java.lang.numberformatexception-input-string.html
[Solved] java.lang.NumberFormatException: For input string | Java Hungry
Any String of length 0 is referred as an empty String in Java. For example: public class JavaHungry { public static void main(String args[]) { String s = ""; int i = Integer.parseInt(s); System.out.println(i); } } Output: Exception in thread "main" java.lang.NumberFormatException: For input ...
🌐
Quora
quora.com › Why-does-parseInt-give-a-number-format-exception
Why does parseInt give a number format exception? - Quora
You don't really have an integer number in that string. It might be empty string as Andrew Blowe, it might have some invalid characters. The only valid characters for the parsing are digits (0123456789) and sign characters (+-). The latter can be...
🌐
CodeAhoy
codeahoy.com › java › Convert-String-To-Int
Java String to int with examples | CodeAhoy
October 22, 2019 - Integer.parseInt(String s) is the ... invalid value i.e. the string does not contain a parsable integer, or it is empty or null, Integer.parseInt(...) will throw NumberFormatException....
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › parseint in java
parseInt in Java: Everything You should Know | upGrad
June 25, 2025 - The parseInt() method in Java can ... and an optional leading minus sign (-) or plus sign (+). The string is empty or consists only of whitespace characters....
Top answer
1 of 16
161

You could return an Integer instead of an int, returning null on parse failure.

It's a shame Java doesn't provide a way of doing this without there being an exception thrown internally though - you can hide the exception (by catching it and returning null), but it could still be a performance issue if you're parsing hundreds of thousands of bits of user-provided data.

EDIT: Code for such a method:

public static Integer tryParse(String text) {
  try {
    return Integer.parseInt(text);
  } catch (NumberFormatException e) {
    return null;
  }
}

Note that I'm not sure off the top of my head what this will do if text is null. You should consider that - if it represents a bug (i.e. your code may well pass an invalid value, but should never pass null) then throwing an exception is appropriate; if it doesn't represent a bug then you should probably just return null as you would for any other invalid value.

Originally this answer used the new Integer(String) constructor; it now uses Integer.parseInt and a boxing operation; in this way small values will end up being boxed to cached Integer objects, making it more efficient in those situations.

2 of 16
41

What behaviour do you expect when it's not a number?

If, for example, you often have a default value to use when the input is not a number, then a method such as this could be useful:

public static int parseWithDefault(String number, int defaultVal) {
  try {
    return Integer.parseInt(number);
  } catch (NumberFormatException e) {
    return defaultVal;
  }
}

Similar methods can be written for different default behaviour when the input can't be parsed.

🌐
Blogger
javarevisited.blogspot.com › 2016 › 08 › javalangnumberformatexception-for-input-string-null-java.html
How to fix java.lang.numberformatexception for input string null - Cause and Solution
On the other hand, you must catch this exception whenever you are trying to convert a String to number e.g. int, short, char, byte, long, float or double as shown below: Here is the right code to convert String to integer in Java with proper exception handling: int number = 0; // or any appllication default value try { number = Integer.parseInt(input); } catch (NumberFormatException nfe) { nfe.printStackTrace(); } The Handing exception is one of the most important coding practices for writing secure and reliable Java programs and this is also used to differentiate an average programmer from a star developer.
Top answer
1 of 6
8

An empty string doesn't represent any integer, so at face value your question has a trivial answer - do not convert an empty string to a valid integer.

The question behind the question seems to be "how to design a robust non-surprising string-to-int conversion utility". This is tricky - consider the following additional cases:

  • What string number representations will you accept? There is more than decimal - the following strings are also integer number representations in common use: 0x0A, 2x10^2, 10E5, 08,...
  • Will you consider converting strings representing floating point numbers to integer numbers?
  • If you answer yes to the previous question, what rounding or truncation will you use?
  • What about rational numbers?
  • You can probably come up with a few more if you tried.

So all in all the actual answer to your question should be to use the string-to-number conversion library functions supplied with your current language and framework, if they have any. If there are no implementations for your situation, then look at other languages or libraries for inspiration of how to handle those cases in your own implementation. If you document what your conversion function does, then it will not (or at least should not) surprise the clients of your function.

2 of 6
2

If you're converting string to int similar to JavaScript's parseInt function, then you would probably want to have '' return NaN or throw an exception, as you said.

JavaScript returns NaN, but if your language doesn't have that convention or feature you should throw an exception or return a null value.