5

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?

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
Nick
  • 61
  • 1
  • 1
  • 3
  • 2
    Your 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 Answers12

15

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")

Jonathon Bolster
  • 15,811
  • 3
  • 43
  • 46
8

You can check whether the following is true: "yourStringHere".matches("\\d+")

dimitrisli
  • 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
5
String s = "abc123";
for(char c : s.toCharArray()) {
    if(Character.isDigit(c)) {
        return true;
    }
}
return false;
Luciano van der Veekens
  • 6,307
  • 4
  • 26
  • 30
user489041
  • 27,916
  • 55
  • 135
  • 204
1

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.

kirchhoff
  • 206
  • 4
  • 12
1

You can use apache StringUtils.isNumeric .

CoolBeans
  • 20,654
  • 10
  • 86
  • 101
0

You could always use Googles Guava

String text = "13567";
CharMatcher charMatcher = CharMatcher.DIGIT;
int output = charMatcher.countIn(text);
Ryan Heitner
  • 13,119
  • 6
  • 77
  • 119
0

That should work:

public static boolean isInteger(String p_str)
{
    if (p_str == null)
        return false;
    else
        return p_str.matches("^\\d*$");
}
fty4
  • 568
  • 9
  • 18
0
  1. User regular expression:

    Pattern.compile("^\\s*\\d+\\s*$").matcher(myString).find();

  2. Just wrap Integer.parse() by try/catch(NumberFormatException)

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
AlexR
  • 114,158
  • 16
  • 130
  • 208
0

You might also want to have a look at java.util.Scanner

Example:

new Scanner("456").nextInt
Pau
  • 803
  • 1
  • 6
  • 12
-1
int number = 0;
try { 
   number = Integer.parseInt(string); 
}
catch(NumberFormatException e) {}
Jon
  • 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
-1

Use the method Integer.parseInt() at http://docs.oracle.com/javase/10/docs/api/java/lang/Integer.html

Nosrep
  • 521
  • 7
  • 21
Freddie
  • 1,717
  • 2
  • 16
  • 23
  • 2
    When 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
-1

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.

Andreas Dolk
  • 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