🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Integer.parseInt, Double.parseDouble, NumberFormat, and Apache Commons Lang.Table of Contents
- Introduction
- Using Regular Expressions
- Using
Integer.parseIntandDouble.parseDouble - Using
NumberFormat - Using
Apache Commons Lang - Conclusion
Introduction
Java provides several ways to check if a string is a number. Depending on your requirements, such as whether you want to check for integers or floating-point numbers, you can use different methods.
Using Regular Expressions
Regular expressions (regex) provide a flexible way to check if a string matches a pattern. You can use regex to determine if a string is a number.
Example
public class NumberCheckExample {
public static void main(String[] args) {
String str1 = "123";
String str2 = "123.45";
String str3 = "abc";
System.out.println(isNumeric(str1)); // true
System.out.println(isNumeric(str2)); // true
System.out.println(isNumeric(str3)); // false
}
public static boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
}
Explanation
str.matches("-?\\d+(\\.\\d+)?"): This regex matches both integers and floating-point numbers, including negative numbers.
Using Integer.parseInt and Double.parseDouble
You can use Integer.parseInt and Double.parseDouble methods to check if a string can be parsed as an integer or a double.
Example
public class NumberCheckExample {
public static void main(String[] args) {
String str1 = "123";
String str2 = "123.45";
String str3 = "abc";
System.out.println(isInteger(str1)); // true
System.out.println(isInteger(str2)); // false
System.out.println(isInteger(str3)); // false
System.out.println(isDouble(str1)); // true
System.out.println(isDouble(str2)); // true
System.out.println(isDouble(str3)); // false
}
public static boolean isInteger(String str) {
try {
Integer.parseInt(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static boolean isDouble(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
}
Explanation
Integer.parseInt(str): Attempts to parse the string as an integer. If it fails, it throws aNumberFormatException.Double.parseDouble(str): Attempts to parse the string as a double. If it fails, it throws aNumberFormatException.
Using NumberFormat
The NumberFormat class provides methods to parse numbers and can be used to check if a string is a number.
Example
import java.text.NumberFormat;
import java.text.ParseException;
public class NumberCheckExample {
public static void main(String[] args) {
String str1 = "123";
String str2 = "123.45";
String str3 = "abc";
System.out.println(isNumber(str1)); // true
System.out.println(isNumber(str2)); // true
System.out.println(isNumber(str3)); // false
}
public static boolean isNumber(String str) {
NumberFormat format = NumberFormat.getInstance();
try {
format.parse(str);
return true;
} catch (ParseException e) {
return false;
}
}
}
Explanation
NumberFormat.getInstance().parse(str): Attempts to parse the string as a number. If it fails, it throws aParseException.
Using Apache Commons Lang
The Apache Commons Lang library provides a utility class NumberUtils that simplifies checking if a string is a number.
Dependency
Add the following dependency to your pom.xml if you are using Maven:
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
Example
import org.apache.commons.lang3.math.NumberUtils;
public class NumberCheckExample {
public static void main(String[] args) {
String str1 = "123";
String str2 = "123.45";
String str3 = "abc";
System.out.println(NumberUtils.isCreatable(str1)); // true
System.out.println(NumberUtils.isCreatable(str2)); // true
System.out.println(NumberUtils.isCreatable(str3)); // false
}
}
Explanation
NumberUtils.isCreatable(str): Checks if the string is a valid Java number.
Conclusion
Checking if a string is a number in Java can be accomplished using various methods, including regular expressions, Integer.parseInt, Double.parseDouble, NumberFormat, and the Apache Commons Lang library. Each method has its own advantages and specific use cases:
- Regular expressions provide a flexible way to match patterns.
Integer.parseIntandDouble.parseDoubleare straightforward for parsing specific number types.NumberFormatoffers a more general approach to parsing numbers.- The
Apache Commons Langlibrary provides utility methods that simplify the process.
By understanding these methods, you can choose the most appropriate one for your specific use case when working with strings in Java.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment