How can I write a method to find whether a given string contains a number? The method should return true if the string contains a number and false otherwise.
This question is related to
java
Using a loop -
public static boolean containsDigit(final String aString)
{
if (aString != null && !aString.isEmpty())
{
for (char c : aString.toCharArray())
{
if (Character.isDigit(c))
{
return true;
}
}
}
return false;
}
Using a stream -
public static boolean containsDigit(final String aString)
{
return aString != null && !aString.isEmpty() &&
aString.chars().anyMatch(Character::isDigit);
}
Another possible solution is to use a Scanner
object like this:
Scanner scanner = new Scanner(inputString);
if (scanner.hasNextInt()) {
return true;
}
else {
return false
}
Of course, if you are looking for a double
, use hasNextDouble()
method (see: Scanner javadoc)
You could use a regex to find out if the String contains a number. Take a look at the matches()
method.
Shef's answer doesn't compile for me. It looks like he's using RegEx in String.contains()
. If you want to use RegEx use this:
String strWithNumber = "This string has a 1 number";
String strWithoutNumber = "This string has a number";
System.out.println(strWithNumber.matches(".*\\d.*"));
System.out.println(strWithoutNumber.matches(".*\\d.*"));
Why don't you try to write a function based on Integer.parseInt(String obj)
? The function could accept as parameter your String object, and then tokenize the String and use Integer.parseInt(String obj)
to extract the number from the "lucky" substring...
Looks like people like doing spoonfeeding, so I have decided to post the worst solution to an easy task:
public static boolean isNumber(String s) throws Exception {
boolean result = false;
byte[] bytes = s.getBytes("ASCII");
int tmp, i = bytes.length;
while (i >0 && (result = ((tmp = bytes[--i] - '0') >= 0) && tmp <= 9));
return result;
}
About the worst code I could imagine, but there might be other people here who can come up with even worse solutions.
Hm, containsNumber is worse:
public static boolean containsNumber(String s) throws Exception {
boolean result = false;
byte[] bytes = s.getBytes("ASCII");
int tmp, i = bytes.length;
while (i >0 && (true | (result |= ((tmp = bytes[--i] - '0') >= 0) && tmp <= 9)));
return result;
}
if(str.matches(".*\\d.*")){
// contains a number
} else{
// does not contain a number
}
Previous suggested solution, which does not work, but brought back because of @Eng.Fouad's request/suggestion.
String strWithNumber = "This string has a 1 number";
String strWithoutNumber = "This string does not have a number";
System.out.println(strWithNumber.contains("\d"));
System.out.println(strWithoutNumber.contains("\d"));
String strWithNumber = "This string has a 1 number";
if(strWithNumber.matches(".*\\d.*")){
System.out.println("'"+strWithNumber+"' contains digit");
} else{
System.out.println("'"+strWithNumber+"' does not contain a digit");
}
String strWithoutNumber = "This string does not have a number";
if(strWithoutNumber.matches(".*\\d.*")){
System.out.println("'"+strWithoutNumber+"' contains digit");
} else{
System.out.println("'"+strWithoutNumber+"' does not contain a digit");
}
Output
'This string has a 1 number' contains digit
'This string does not have a number' does not contain a digit
try{
Integer.parseInt(string);
return true;
}catch (Exception e){
return false;
}
or you can do it himself:
for ( int i = 0; i < string.length; ++i ) {
if ( !( string[i] >= '0' || string[i] <= '9' ) )
return false;
}
return true;
Of course is also function isDigit
Source: Stackoverflow.com