Home » Core Java » isDigit Java Example

About Mary Zheng

Mary Zheng
Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.

isDigit Java Example

1. Introduction

Java provides a java.lang.Character class to wrap a value of the primitive type char in an object. It has provided several methods for determining a character’s category, such as lowercase letter, digit, etc since version 5. Here are the isDigit method’s signatures:

//Determines if the specified character is a digit.
static boolean	isDigit​(char ch)	

//Determines if the specified character (Unicode code point) is a digit.
static boolean	isDigit​(int codePoint)	

The isDigit method takes a single argument of primitive type int or char and returns true if it is a digit. Please note that the int value is the Unicode code point. Click here for the code point values.

In this example, I will demonstrate how to use the isDigit method to check a given character is a digit or not.

2. Technologies Used

The example code in this article was built and run using:

  • Java 11
  • Maven 3.3.9
  • Eclipse Oxygen
  • Junit 4.12

3. Maven Project

3.1 Dependencies

I will include Junit in the pom.xml.

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>jcg.zheng.demo</groupId>
	<artifactId>java-isdigit-demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<build>
		<sourceDirectory>src</sourceDirectory>
		<plugins>
			<plugin>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>3.8.0</version>
				<configuration>
					<release>11</release>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
	</dependencies>
</project>

3.2 JUnit Test

In this step, I will create an IsDigitTest class to show how to check a given character or code point is a digit or not.

  • isDigit_codePointTest() – tests the code point of 54 which is for ‘6’ and 65 which is for ‘A’.
  • isDigit_charTest() – tests the char of ‘1’ and ‘B’.

IsDigitTest.java

package jcg.zheng.demo;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class IsDigitTest {

	@Test
	public void isDigit_codePointTest() {
		int sixCodePoint = 54;
		assertTrue(Character.isDigit(sixCodePoint));
		assertEquals(6, Character.getNumericValue(sixCodePoint));

		int letterACodePoint = 65;
		assertFalse(Character.isDigit(letterACodePoint));
		assertEquals('A', (char)letterACodePoint);

	}

	@Test
	public void isDigit_charTest() {
		assertTrue(Character.isDigit('1'));
		assertFalse(Character.isDigit('B'));
	}

}

Execute C:\MaryZheng\Workspaces\jdk12\java-isdigit-demo>mvn test -Dtest=IsDigitTest and capture output here.

Output

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running jcg.zheng.demo.IsDigitTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.116 sec

Results :

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

3.3 Demo Application

In this step, I will create a java application which checks a given string’s every character is a digit or not.

I will use the IntStream class to get the characters and its code point, and then check with isDigit method.

DemoApp.java

package jcg.zheng.demo;

public class DemoApp {

	private static String TEST_STRING = "ASomeStringWithDigit0123456789.*&?";

	public static void main(String[] args) {

		TEST_STRING.codePoints().forEach(codePoint -> {
			printCodePoint(codePoint);
		});

		TEST_STRING.chars().forEach(codePoint -> {
			printCodePoint(codePoint);
		});

		for (char character : TEST_STRING.toCharArray()) {
			System.out.printf("\nisDigit(%s) = %s",  character, Character.isDigit(character));
		}

	}

	private static void printCodePoint(int codePoint) {
		System.out.printf("\nCodePoint Name: %s, type: %s,  isDigit(%d)= %s", Character.getName(codePoint), Character.getType(codePoint), codePoint, Character.isDigit(codePoint));
	}
}

Execute DemoApp as a Java application and capture the output here.

Output

CodePoint Name: LATIN CAPITAL LETTER A, type: 1,  isDigit(65)= false
CodePoint Name: LATIN CAPITAL LETTER S, type: 1,  isDigit(83)= false
CodePoint Name: LATIN SMALL LETTER O, type: 2,  isDigit(111)= false
CodePoint Name: LATIN SMALL LETTER M, type: 2,  isDigit(109)= false
CodePoint Name: LATIN SMALL LETTER E, type: 2,  isDigit(101)= false
CodePoint Name: LATIN CAPITAL LETTER S, type: 1,  isDigit(83)= false
CodePoint Name: LATIN SMALL LETTER T, type: 2,  isDigit(116)= false
CodePoint Name: LATIN SMALL LETTER R, type: 2,  isDigit(114)= false
CodePoint Name: LATIN SMALL LETTER I, type: 2,  isDigit(105)= false
CodePoint Name: LATIN SMALL LETTER N, type: 2,  isDigit(110)= false
CodePoint Name: LATIN SMALL LETTER G, type: 2,  isDigit(103)= false
CodePoint Name: LATIN CAPITAL LETTER W, type: 1,  isDigit(87)= false
CodePoint Name: LATIN SMALL LETTER I, type: 2,  isDigit(105)= false
CodePoint Name: LATIN SMALL LETTER T, type: 2,  isDigit(116)= false
CodePoint Name: LATIN SMALL LETTER H, type: 2,  isDigit(104)= false
CodePoint Name: LATIN CAPITAL LETTER D, type: 1,  isDigit(68)= false
CodePoint Name: LATIN SMALL LETTER I, type: 2,  isDigit(105)= false
CodePoint Name: LATIN SMALL LETTER G, type: 2,  isDigit(103)= false
CodePoint Name: LATIN SMALL LETTER I, type: 2,  isDigit(105)= false
CodePoint Name: LATIN SMALL LETTER T, type: 2,  isDigit(116)= false
CodePoint Name: DIGIT ZERO, type: 9,  isDigit(48)= true
CodePoint Name: DIGIT ONE, type: 9,  isDigit(49)= true
CodePoint Name: DIGIT TWO, type: 9,  isDigit(50)= true
CodePoint Name: DIGIT THREE, type: 9,  isDigit(51)= true
CodePoint Name: DIGIT FOUR, type: 9,  isDigit(52)= true
CodePoint Name: DIGIT FIVE, type: 9,  isDigit(53)= true
CodePoint Name: DIGIT SIX, type: 9,  isDigit(54)= true
CodePoint Name: DIGIT SEVEN, type: 9,  isDigit(55)= true
CodePoint Name: DIGIT EIGHT, type: 9,  isDigit(56)= true
CodePoint Name: DIGIT NINE, type: 9,  isDigit(57)= true
CodePoint Name: FULL STOP, type: 24,  isDigit(46)= false
CodePoint Name: ASTERISK, type: 24,  isDigit(42)= false
CodePoint Name: AMPERSAND, type: 24,  isDigit(38)= false
CodePoint Name: QUESTION MARK, type: 24,  isDigit(63)= false
CodePoint Name: LATIN CAPITAL LETTER A, type: 1,  isDigit(65)= false
CodePoint Name: LATIN CAPITAL LETTER S, type: 1,  isDigit(83)= false
CodePoint Name: LATIN SMALL LETTER O, type: 2,  isDigit(111)= false
CodePoint Name: LATIN SMALL LETTER M, type: 2,  isDigit(109)= false
CodePoint Name: LATIN SMALL LETTER E, type: 2,  isDigit(101)= false
CodePoint Name: LATIN CAPITAL LETTER S, type: 1,  isDigit(83)= false
CodePoint Name: LATIN SMALL LETTER T, type: 2,  isDigit(116)= false
CodePoint Name: LATIN SMALL LETTER R, type: 2,  isDigit(114)= false
CodePoint Name: LATIN SMALL LETTER I, type: 2,  isDigit(105)= false
CodePoint Name: LATIN SMALL LETTER N, type: 2,  isDigit(110)= false
CodePoint Name: LATIN SMALL LETTER G, type: 2,  isDigit(103)= false
CodePoint Name: LATIN CAPITAL LETTER W, type: 1,  isDigit(87)= false
CodePoint Name: LATIN SMALL LETTER I, type: 2,  isDigit(105)= false
CodePoint Name: LATIN SMALL LETTER T, type: 2,  isDigit(116)= false
CodePoint Name: LATIN SMALL LETTER H, type: 2,  isDigit(104)= false
CodePoint Name: LATIN CAPITAL LETTER D, type: 1,  isDigit(68)= false
CodePoint Name: LATIN SMALL LETTER I, type: 2,  isDigit(105)= false
CodePoint Name: LATIN SMALL LETTER G, type: 2,  isDigit(103)= false
CodePoint Name: LATIN SMALL LETTER I, type: 2,  isDigit(105)= false
CodePoint Name: LATIN SMALL LETTER T, type: 2,  isDigit(116)= false
CodePoint Name: DIGIT ZERO, type: 9,  isDigit(48)= true
CodePoint Name: DIGIT ONE, type: 9,  isDigit(49)= true
CodePoint Name: DIGIT TWO, type: 9,  isDigit(50)= true
CodePoint Name: DIGIT THREE, type: 9,  isDigit(51)= true
CodePoint Name: DIGIT FOUR, type: 9,  isDigit(52)= true
CodePoint Name: DIGIT FIVE, type: 9,  isDigit(53)= true
CodePoint Name: DIGIT SIX, type: 9,  isDigit(54)= true
CodePoint Name: DIGIT SEVEN, type: 9,  isDigit(55)= true
CodePoint Name: DIGIT EIGHT, type: 9,  isDigit(56)= true
CodePoint Name: DIGIT NINE, type: 9,  isDigit(57)= true
CodePoint Name: FULL STOP, type: 24,  isDigit(46)= false
CodePoint Name: ASTERISK, type: 24,  isDigit(42)= false
CodePoint Name: AMPERSAND, type: 24,  isDigit(38)= false
CodePoint Name: QUESTION MARK, type: 24,  isDigit(63)= false
isDigit(A) = false
isDigit(S) = false
isDigit(o) = false
isDigit(m) = false
isDigit(e) = false
isDigit(S) = false
isDigit(t) = false
isDigit(r) = false
isDigit(i) = false
isDigit(n) = false
isDigit(g) = false
isDigit(W) = false
isDigit(i) = false
isDigit(t) = false
isDigit(h) = false
isDigit(D) = false
isDigit(i) = false
isDigit(g) = false
isDigit(i) = false
isDigit(t) = false
isDigit(0) = true
isDigit(1) = true
isDigit(2) = true
isDigit(3) = true
isDigit(4) = true
isDigit(5) = true
isDigit(6) = true
isDigit(7) = true
isDigit(8) = true
isDigit(9) = true
isDigit(.) = false
isDigit(*) = false
isDigit(&) = false
isDigit(?) = false

4. isDigit Java Example – Summary

In this example, I demonstrate how to use isDigit method to check a given character is a digit or not.

5. Download the Source Code

This example consists of a Java application to demonstrate the isDigit method.

Download
You can download the full source code of this example here: isDigit Java Example
(+2 rating, 2 votes)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of