Welcome to the World of Online Learning:
Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Write a Java Program to Check if a String is Numeric
PROGRAM: Java Program to Check if a String is Numeric
/*Java Program to Check if a String is Numeric*/
1: Check if a string is numeric
public class Numeric {
public static void main(String[] args) {
String string = “12345.15”;
boolean numeric = true;
try {
Double num = Double.parseDouble(string);
} catch (NumberFormatException e) {
numeric = false;
}
if(numeric)
System.out.println(string + ” is a number”);
else
System.out.println(string + ” is not a number”);
}
}
2: Check if a string is numeric or not using regular expressions (regex)
public class Numeric {
public static void main(String[] args) {
String string = “-1234.15”;
boolean numeric = true;
numeric = string.matches(“-?\\d+(\\.\\d+)?”);
if(numeric)
System.out.println(string + ” is a number”);
else
System.out.println(string + ” is not a number”);
}
}