如何在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.pswp.cn/news/541366.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541366.shtml
英文地址,請注明出處:http://en.pswp.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…