Java 程序检查字符串是否为数字
示例
您可以根据以下程序检查给定字符串是否为数字。
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");
}
}
}输出
Enter a string :: 4245 Given string is a number
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP