Java programming Exercises: 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 a 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 :
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.
Java: Tips of the Day
What is the best way to filter a Java Collection?
Java 8 (2014) solves this problem using streams and lambdas in one line of code:
List<Person> beerDrinkers = persons.stream() .filter(p -> p.getAge() > 16).collect(Collectors.toList());
Use Collection#removeIf to modify the collection in place. (Notice: In this case, the predicate will remove objects who satisfy the predicate):
persons.removeIf(p -> p.getAge() <= 16);
lambdaj allows filtering collections without writing loops or inner classes:
ListbeerDrinkers = select(persons, having(on(Person.class).getAge(), greaterThan(16)));
Ref: https://bit.ly/3uwYid6
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework