w3resource

Java: Check whether a given string is a valid hex code or not

Java Regular Expression: Exercise-26 with Solution

Write a Java program to check whether a given string is valid hex code or not.

A hexadecimal color value is a six-digit code preceded by a # sign, and is exactly 6 characters in length. Each character must be an alphabetic character from A-F (uppercase or lowercase.) or a digit from 0-9.

Sample Solution:

Java Code:

public class test {
 
   public static void main(String[] args) {	   
	    String text = "123456";
		System.out.println("Original String: "+text);
		System.out.println("Check the said string is a valid hex code or not? "+validate(text));
		text = "#eaecff";
		System.out.println("\nOriginal String: "+text);
		System.out.println("Check the said string is a valid hex code or not? "+validate(text));
		text = "#FF0000";
		System.out.println("\nOriginal String: "+text);
		System.out.println("Check the said string is a valid hex code or not? "+validate(text));
		text = "#DD5C5C";
		System.out.println("\nOriginal String: "+text);
		System.out.println("Check the said string is a valid hex code or not? "+validate(text));
		text = "#0000000";
		System.out.println("\nOriginal String: "+text);
		System.out.println("Check the said string is a valid hex code or not? "+validate(text));		
	}

   public static boolean validate(String text) {
	   return text.matches("#[0-9A-Fa-f]{6}");
   }
}

Sample Output:

Original String: 123456
Check the said string is a valid hex code or not? false

Original String: #eaecff
Check the said string is a valid hex code or not? true

Original String: #FF0000
Check the said string is a valid hex code or not? true

Original String: #DD5C5C
Check the said string is a valid hex code or not? true

Original String: #0000000
Check the said string is a valid hex code or not? false

Flowchart :

Flowchart: Last n vowels of a given string.

Java Code Editor:

Contribute your code and comments through Disqus.

Previous: Last n vowels of a given string.

Next: Add a dash before and after every vowel in a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/java-exercises/re/java-re-exercise-26.php