Languages
[Edit]
EN

Java - check if string only contains numbers

0 points
Created by:
Lia-Perez
565

In this article, we would like to show you how to check if the string only contains numbers in Java.

1. Using regex

Edit

In this example, we use a regular expression (regex) with Pattern.matcher() to check if the strings contain only numbers.

Output:

2. Using Character.isDigit()

Edit

In this example, we create a function that loops through the string and checks if each character is a digit with Character.isDigit(char ch) method.

Practical example:

Output:

1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - String (popular problems)

Java - check if string only contains numbers
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join