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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/541366.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

oracle清理告警日志,Oracle 跟踪/告警/监听日志的清理脚本

[root ~]# cat del_oracle_log.sh#!/bin/bashsource /home/oracle/.bash_profilefunction audit_log(){ #---audit_log日志跟踪文件#audit_log$(strings $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora|grep -i audit_file_dest|awk -F {print $NF}|sed "s///g")audit_lo…

python爬虫之scrapy框架

Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架。 其可以应用在数据挖掘,信息处理或存储历史数据等一系列的程序中。其最初是为了页面抓取 (更确切来说, 网络抓取 )所设计的, 也可以应用在获取API所返回的数据(例如 Amazon As…

oracle中的事务回滚,ORACLE 死事务的回滚

死事务出现在异常关闭数据库或者事务进程不正常结束,比如KILL -9,shutdown abort的情况下。当前数据库里的死事务可以通过查询内部表x$ktuxe来获得。select ADDR,KTUXEUSN,KTUXESLT,KTUXESQN,KTUXESIZ from x$ktuxe where KTUXECFLDEAD;ADDR …

大数据数据可视化设计原则_数据可视化设计的8顶帽子

大数据数据可视化设计原则8 hats of data visualization are basically the important persons and their roles that are basically required to carry out data visualization are as follows: 数据可视化有8个基本要素,而进行数据可视化所需的基本角色如下&#…

debian8.8安装谷歌浏览器

第一步:下载: wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb //32位 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb //64位第二步:安装dpkg -i google-chrome*.deb…

MB_LEN_MAX常数,带C ++示例

C MB_LEN_MAX宏常量 (C MB_LEN_MAX macro constant) MB_LEN_MAX constant is a macro constant which is defied in climits header, it is used to get the maximum number of bytes in a multibyte character, for any locale, it returns maximum number of bytes that a m…

php谷歌收录接口,php实现查询百度google收录情况(示例代码)

PHP$SEOdetail array();$domain !empty($_GET[q]) ? $_GET[q] : www.mycodes.net;baidudetail($domain);googledetail($domain);var_dump($SEOdetail);function baidudetail($domain) {$baidu_site http://www.baidu.com/baidu?wordsite%3A . $domain;$baidu_link http:/…

Linux学习第三步(Centos7安装mysql5.7数据库)

版本:mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar 前言:在linux下安装mysql不如windows下面那么简单,但是也不是很难。本文向大家讲解了如何在Centos7下如何安装mysql5.7版本,如果有什么问题和错误的地方,欢迎大家指出。 注释&…

linux oracle删除恢复数据恢复,Linux下Oracle误删除数据文件恢复操作

检查数据文件的位置如下:SQL> select name from v$datafile;NAME--------------------------------------------------------------------------------/u01/app/Oracle/oradata/marven/system01.dbf/u01/app/oracle/oradata/marven/undotbs1.dbf/u01/app/oracle/…

数据库如何处理数据库太大_网络数据库中的数据处理

数据库如何处理数据库太大Before learning the data manipulation in a network model, we are discussing data manipulation language, so what is the data manipulation language? 在学习网络模型中的数据操作之前,我们正在讨论数据操作语言,那么什…

oracle12537错误,ORA-12537:TNS:connection closed错误处理方法

1.ORA-12537:TNS:connection closed错误处理过程检查监听正常,Oracle服务也是正常启动的,但是登录不进去。2.解决方案1. cd $ORACLE_HOME/bin/ 进入bin目录2. ll oracle-rwxrwxrwx. 1 ora12 dba 323762222 6?. 14 19:12 oracle3.chmod 6571 oracle 更改…

操作系统中的死锁_操作系统中的死锁介绍

操作系统中的死锁1.1究竟什么是僵局? (1.1 What exactly is a deadlock?) In a multiprogramming environment, there may be several processes with a finite number of resources. A process may request another resource while still holding some of the oth…

《云数据管理:挑战与机遇》2.3 数据库系统

本节书摘来自华章出版社《云数据管理》一书中的第2章,第3节,作者迪卫艾肯特阿格拉沃尔,更多章节内容可以访问云栖社区“华章计算机”公众号查看本节中,我们将为数据库系统中的一些主要概念提供一个相当抽象、简洁和高层次的描述。…

sql server与oracle的分页,详解SQLServer和Oracle的分页查询

不管是DRP中的分页查询代码的实现还是面试题中看到的关于分页查询的考察,都给我一个提示:分页查询是重要的。当数据量大的时候是必须考虑的。之前一直没有花时间停下来好好总结这里。现在又将Oracle视频中关于分页查询的内容看了一遍,发现很容…

java treemap_Java TreeMap lastEntry()方法与示例

java treemapTreeMap类的lastEntry()方法 (TreeMap Class lastEntry() method) lastEntry() method is available in java.util package. lastEntry()方法在java.util包中可用。 lastEntry() method is used to return the entry (key-value pairs) that exists with the large…

LeetCode OJ 之 Valid Anagram

题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example,s "anagram", t "nagaram", return true.s "rat", t "car", return false. Note: You may assume the string…

oracle光标位置无效,解决在Form表单中光标移动不了问题

apply p8727236_10123 for Developer Suite 10.1.2.3 in Linux首先到oracle的技术支持下载所需补丁,然后1先打补丁7121788,把p7121788_10123_LINUX.zip解压到/home/oracledev目录下(ORACLE_HOME为/u01/app/oracledev/OraHome_dev)$cd /home/oracledev/7121788$expo…

java treemap_Java TreeMap HigherKey()方法与示例

java treemapTreeMap类HigherKey()方法 (TreeMap Class higherKey() method) higherKey() method is available in java.util package. HigherKey()方法在java.util包中可用。 higherKey() method is used to return the lowest key value element higher than the given key e…

centos配置ipv6地址

首先打开网站注册一个账号:http://www.tunnelbroker.net创建一个ipv6的地址:把下面的命令在linux上执行一遍,这个方式是临时生效,重启网卡和重启系统自动失效。把上面的命令保存到一个配置文件中:vi /etc/sysconfig/ne…