Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Java Program to Check if a String is Numeric
Example
You can check if a given string is Numeric as shown in the following program.
import java.util.Scanner;
public class StringNumeric {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string ::");
String str = sc.next();
boolean number = str.matches("-?\d+(\.\d+)?");
if(number) {
System.out.println("Given string is a number");
} else {
System.out.println("Given string is not a number");
}
}
}
Output
Enter a string :: 4245 Given string is a number
Advertisements
