xxxxxxxxxx
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static void main(String[] args) {
String input = "123.45";
if (isNumeric(input)) {
System.out.println("The string is a number.");
} else {
System.out.println("The string is not a number.");
}
}
xxxxxxxxxx
public static boolean isInt(String str) {
try {
@SuppressWarnings("unused")
int x = Integer.parseInt(str);
return true; //String is an Integer
} catch (NumberFormatException e) {
return false; //String is not an Integer
}
}
xxxxxxxxxx
String someString = "123123";
boolean isNumeric = someString.chars().allMatch( Character::isDigit );
xxxxxxxxxx
private boolean isInt(String s){
try{
Integer.parseInt(s);
return true;
}catch (NumberFormatException e){
return false;
}
}
xxxxxxxxxx
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
xxxxxxxxxx
public boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
xxxxxxxxxx
public class ContainsExample {
public static void main(String args[]){
String sample = "krishna64";
char[] chars = sample.toCharArray();
StringBuilder sb = new StringBuilder();
for(char c : chars){
if(Character.isDigit(c)){
sb.append(c);
}
}
System.out.println(sb);
}
xxxxxxxxxx
string str = "3257fg";
for(int i = 0;i < (strlen(str) - 1);i++) {
if((int)str[i] < 10) {
// it is a number, so do some code
} else {
// it is not a number, do something else
}
}