We will check whether string is a number or not – with the help of logic we will solve this problem,

我们将检查字符串是否为数字-借助逻辑,我们将解决此问题,

  • In the first step, we will take a string variable named str and store any value in it.

    第一步,我们将使用一个名为str的字符串变量,并将任何值存储在其中。

  • In the second step, We will take a boolean variable named str_numeric which stores Boolean value like true or false. Let us suppose that given string is numeric so that initially boolean variable str_numeric is set to true.

    在第二步中,我们将使用一个名为str_numeric的布尔变量,该变量存储布尔值(如true或false) 。 让我们假设给定的字符串是数字,因此最初的布尔变量str_numeric设置为true。

  • In the third step we will do one thing in the try block we will convert String variable to Double by using parseDouble() method because initially we are assuming that given the string is number that's why we are converting first.

    在第三步中,我们将在try块中做一件事,我们将使用parseDouble()方法将String变量转换为Double,因为最初我们假设给定的字符串是数字,这就是我们首先进行转换的原因。

  • If it throws an error (i.e. NumberFormatException), it means given String is not a number and then at the same time boolean variable str_numeric is set to false. Otherwise given string is a number.

    如果抛出错误(即NumberFormatException ),则意味着给定的String不是数字,然后将布尔变量str_numeric设置为false 。 否则,给定的字符串是一个数字。

Example:

例:

public class IsStringNumeric {public static void main(String[] args) {// We have initialized a string variable with double values
String str1 = "1248.258";
// We have initialized a Boolean variable and
// initially we are assuming that string is a number
// so that the value is set to true
boolean str_numeric = true;
try {// Here we are converting string to double
// and why we are taking double because
// it is a large data type in numbers and
// if we take integer then we can't work
// with double values because we can't covert
// double to int then, in that case,
// we will get an exception so that Boolean variable
// is set to false that means we will get wrong results.
Double num1 = Double.parseDouble(str1);
}
// Here it will raise an exception
// when given input string is not a number
// then the Boolean variable is set to false.
catch (NumberFormatException e) {str_numeric = false;
}
// if will execute when given string is a number
if (str_numeric)
System.out.println(str1 + " is a number");
// Else will execute when given string is not a number
else
System.out.println(str1 + " is not a number");
}
}

Output

输出量

D:\Programs>javac IsStringNumeric.java
D:\Programs>java IsStringNumeric
1248.258 is a number

翻译自: https://www.includehelp.com/java/how-to-check-if-string-is-number-in-java.aspx

如何在Java中检查字符串是否为数字?相关推荐

  1. 如何在Java中检查字符串是否为数字

    在解析字符串之前,如何检查字符串是否为数字? #1楼 解析它(即使用Integer#parseInt )并简单地捕获异常. =) 需要澄清的是:parseInt函数检查它是否可以在任何情况下(显然)都 ...

  2. java中如何检查字符串都是数字_如何在Java中检查字符串是否为数字?

    我们将检查字符串是否为数字-借助逻辑,我们将解决此问题,第一步,我们将使用一个名为str的字符串变量,并将任何值存储在其中. 在第二步中,我们将使用一个名为str_numeric的布尔变量,该变量存储 ...

  3. python字符串转浮点数_如何在Python中检查字符串是否为数字(浮点数)?

    python字符串转浮点数 Using python it is very to interconvert the datatypes of a variable. A string can be e ...

  4. 字符串压缩 java_如何在Java中压缩字符串?

    如何在Java中压缩字符串? 我使用GZIPOutputStream或ZIPOutputStream压缩字符串(我的2222235278130938882小于20),但压缩结果比原始字符串长. 在某个 ...

  5. java中判断字符串是否为数字的方法 StringUtil包函数

    java中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){   for (int i = 0; i &l ...

  6. java中判断字符串是否为数字(正整数)

    标题:java中判断字符串是否为数字(正整数) 遍历s的每一个字符 private boolean isNumeric(String s) {for (int i = 0; i < s.leng ...

  7. 怎么比较字符串java_如何在Java中比较字符串?

    在本文中,优锐课将带你学习如何比较字符串以及使用等于(=)运算符比较字符串时发生的问题. 介绍 字符串是Java中的特殊类.我们在Java程序中定期使用String,因此比较两个字符串是Java中的一 ...

  8. java 保留字符串,如何在Java中保留字符串而不使用反转功能

    有以下几种在Java中反转字符串的方法: 使用for循环 使用While循环 使用静态方法 使用For循环 使用for循环在Java中反转字符串的示例 在下面的示例中, 我们使用了for循环来反转字符 ...

  9. 如何在Java中分割字符串

    我有一个字符串"004-034556" ,我想分成两个字符串: string1="004"; string2="034556"; 这意味着第 ...

最新文章

  1. LeetCode简单题之按照频率将数组升序排序
  2. 修改IDEA项目的JDK应用路径
  3. [LOJ 6288]猫咪[CF 700E]Cool Slogans
  4. selenium python_Python+Selenium基础入门及实践
  5. 在 Visual Studio 2019 中为 .NET Core WinForm App 启用窗体设计器
  6. mysql快速随机_MySQL随机取数据最高效的方法
  7. BOM(Browser Object Model)
  8. first review of team blog(4.26)
  9. 信息学奥赛一本通(1241:二分法求函数的零点)
  10. python教程输入_输入/输出
  11. ubuntu--雷鸟只能收邮件不能发邮件
  12. openssh服务以及基于ssh的双机互信配置
  13. Arduino控制微小的六足3D打印机器人
  14. H265解码流程理解
  15. VBA版本获取ClientKey
  16. 前端工具使用记录(css/js/htm)
  17. 错觉图片生成实验 - 隐形的黑点
  18. 敷衍的面试|记录问题仅供参考,不代表最终答案
  19. 解决使用Ajax 前台提交表单到后台,但输出为问号的中文乱码情况
  20. 传图识字有次数限制吗_告诉你一个免费的微信小程序,可以传图识字

热门文章

  1. poj1062昂贵的聘礼(Dijkstra**)
  2. python数独游戏源代码100行_python实现自动解数独小程序
  3. 大学php老师,php高校教师总结计划系统
  4. android seekbar闪退,android seekbar 踩坑之路
  5. 俩台电脑怎么设置同一局域网_方法 | 把手机上的照片传到电脑上
  6. xtrabackup备份脚本
  7. 安装LibreOffice和字体
  8. 高性能MySQL(2)——Schema与数据类型的优化
  9. inux CentOS 7 修改内核启动默认顺序
  10. MySQL 直接存储图片并在 html 页面中展示,点击下载