-
/*Check if string contains valid number example.This example shows how to check if string contains valid numberor not using parseDouble and parseInteger methods ofDouble and Integer wrapper classes.*/
public class CheckValidNumberExample {
public static void main(String[] args) {
String[] str = new String[]{“10.20”, “123456”, “12.invalid”};
for(int i=0 ; i < str.length ; i ++){
if( str[i].indexOf(“.”) > 0 ){
try{/** To check if the number is valid decimal number, use* double parseDouble(String str) method of* Double wrapper class.** This method throws NumberFormatException if the* argument string is not a valid decimal number.*/Double.parseDouble(str[i]);System.out.println(str[i] + ” is a valid decimal number”);}catch(NumberFormatException nme){System.out.println(str[i] + ” is not a valid decimal number”);}
}else{try{/** To check if the number is valid integer number, use* int parseInt(String str) method of* Integer wrapper class.** This method throws NumberFormatException if the* argument string is not a valid integer number.*/
Integer.parseInt(str[i]);System.out.println(str[i] + ” is valid integer number”);}catch(NumberFormatException nme){System.out.println(str[i] + ” is not a valid integer number”);}}}
}}
/*Output would be10.20 is a valid decimal number123456 is valid integer number12.invalid is not a valid decimal number*/
Friday, 13 April 2018
Popular Posts
-
Build interactive, data-driven websites with the potent combination of open-source technologies and web standards, even if you have only ...
-
Advantages of Object Oriented Programming Object oriented programming has several advantage to the programmer and user. Through inheri...
-
What is Python? → Python is an interpreted High level programming Language for General purpose programming. Python is created by Gui...
-
Java :- It is a fast, secure and reliable general purpose computer programming language. Python :- A Readable, efficient and powerfu...
-
Python is a high-level programming language that is widely used in various kinds of programming activities. Python is known for its objec...
-
Activity_main.xml File <? xml version= "1.0" encoding= "utf-8" ?> < LinearLayout xmlns: android = &qu...
-
Business Analytics Definition "Study of business data using statistical technique and programming for creating decision suppo...
-
Variables in R A variables are nothing but reserved memory locations to store values. This means that when you create a variab...
-
➤Each family of processes has its own set of instructions, the set of instructions are called machine instructions ➤A processor underst...
-
Procedure Oriented Programming Language:- High level language such as COBOL , FORTRAN AND C is commonly known as procedure oriented progr...

Categories
Android
(21)
AngularJS
(1)
Assembly Language
(2)
Books
(10)
C
(75)
C#
(4)
C++
(81)
Course
(1)
Data Strucures
(4)
Downloads
(1)
Engineering
(13)
flutter
(1)
FPL
(17)
Hadoop
(1)
HTML&CSS
(38)
IS
(25)
Java
(87)
Leet Code
(4)
PHP
(20)
Projects
(1)
Python
(218)
R
(69)
Selenium Webdriver
(2)
Software
(14)
SQL
(27)

ReplyDeletenice article for beginners.thank you.
javacodegeeks
welookups java