Say you have a string that you want to test to make sure that it contains an integer before you proceed with other the rest of the code. What would you use, in java, to find out whether or not it is an integer?
-
2Your question isn't clear. Do you want to check whether it contains an integer or with it is an integer? abc123 or 123456? – Ash Burlaczenko Dec 08 '10 at 14:30
-
@Ash **to find out whether or not it is an integer** - to me, the question is clear (the title is ambigious). – Andreas Dolk Dec 08 '10 at 14:58
-
You may also want to look at this http://stackoverflow.com/questions/3517686/cannot-convert-string-to-integer-in-java – CoolBeans Dec 08 '10 at 15:29
12 Answers
If you want to make sure that it is only an integer and convert it to one, I would use parseInt in a try/catch
. However, if you want to check if the string contains a number then you would be better to use the String.matches with Regular Expressions: stringVariable.matches("\\d")

- 15,811
- 3
- 43
- 46
You can check whether the following is true: "yourStringHere".matches("\\d+")

- 20,895
- 12
- 59
- 63
-
As a clarification, this checks if your string matches the regular expression "\\d+" which is one or more digits [0-9]. – Seabass77 Jan 31 '19 at 21:27
String s = "abc123";
for(char c : s.toCharArray()) {
if(Character.isDigit(c)) {
return true;
}
}
return false;

- 6,307
- 4
- 26
- 30

- 27,916
- 55
- 135
- 204
I use the method matches() from the String class:
Scanner input = new Scanner(System.in)
String lectura;
int number;
lectura = input.next();
if(lectura.matches("[0-3]")){
number = lectura;
}
This way you can also validate that the range of the numbers is correct.

- 206
- 4
- 12
You could always use Googles Guava
String text = "13567";
CharMatcher charMatcher = CharMatcher.DIGIT;
int output = charMatcher.countIn(text);

- 13,119
- 6
- 77
- 119
That should work:
public static boolean isInteger(String p_str)
{
if (p_str == null)
return false;
else
return p_str.matches("^\\d*$");
}

- 568
- 9
- 18
User regular expression:
Pattern.compile("^\\s*\\d+\\s*$").matcher(myString).find();
Just wrap Integer.parse() by try/catch(NumberFormatException)

- 113,398
- 19
- 180
- 268

- 114,158
- 16
- 130
- 208
-
-
-
It is ARABIC-INDIC DIGIT ONE. Try for yourself: `System.out.println(Integer.valueOf("١"));` – Michael Konietzka Dec 08 '10 at 17:45
int number = 0;
try {
number = Integer.parseInt(string);
}
catch(NumberFormatException e) {}

- 1
-
Welcome to stackoverflow. This question is old and was already answered. Typically, it is best not to resurrect stale threads unless your response contributes something significantly new or different over previous answers. – oers Oct 26 '12 at 12:24
Use the method Integer.parseInt() at http://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html
-
2When linking to a version of the API, I would suggesting using a more up to date version. – jzd Dec 08 '10 at 14:37
-
`Integer.parseInt("8.0")` throws an exception, despite 8.0 can fit into an int without loss – Phlip Feb 28 '23 at 23:09
If you just want to test, if a String contains an integer value only, write a method like this:
public boolean isInteger(String s) {
boolean result = false;
try {
Integer.parseInt("-1234");
result = true;
} catch (NumberFormatException nfe) {
// no need to handle the exception
}
return result;
}
parseInt
will return the int
value (-1234 in this example) or throw an exception.

- 113,398
- 19
- 180
- 268
-
`NumberFormatException`is a `RuntimeExcption`. Will catching a RuntimeException not be dangerous or "evil", for example when used in transactions, which maybe marked as rollback because a RuntimeException was thrown? – Michael Konietzka Dec 09 '10 at 06:55