Skip to content

Instantly share code, notes, and snippets.

@javamultiplex
Created September 20, 2017 11:35
Show Gist options
  • Save javamultiplex/174b4e7f0db13271a5ebe87bfb846844 to your computer and use it in GitHub Desktop.
Save javamultiplex/174b4e7f0db13271a5ebe87bfb846844 to your computer and use it in GitHub Desktop.
Given Number is Hexadecimal or not?
package com.javamultiplex.number;
import java.util.Scanner;
/**
*
* @author Rohit Agarwal
* @category Number Problems
* @problem Number is hexadecimal or not
*
*/
public class HexadecimalNumber {
public static void main(String[] args) {
Scanner input = null;
try {
input = new Scanner(System.in);
System.out.println("Enter number : ");
String number = input.next();
/*
* Regular expression that matches string containing digits [0-9]
* and alphabets [A-F] or [a-f].
*/
String pattern1 = "^[0-9A-Fa-f]+$";
if (number.matches(pattern1)) {
System.out.println("It is hexadecimal");
} else {
System.out.println("It is not hexadecimal.");
}
} finally {
if (input != null) {
input.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment