Write a program to read a string and validate whether the given string is a valid color code based on the following rules:
- Must start with “#” symbol
- Must contain six characters after #
- It may contain alphabets from A-F or digits from 0-9.
The return type (integer) should return 1 if the color is as per the rules else return -1.
Input and Output Format
- Input consists of a string.
- The output consists of a string (Valid or Invalid).
Refer sample output for formatting specifications
Sample Input 1:
#FF9922
Sample Output 1:
Valid
Sample Input 2:
#FF9(22
Sample Output 2:
Invalid
Validate Color Code Program in Java
Following are the steps to validate codes of color:
- Input string from the user.
- Pass the string to colorCodeValidation() method.
- Inside the method, declare and initialize variables b and b1 to false.
- Now, return the sub-string of the specified string and store it in string s2.
- Now, check for the below-mentioned condition
- First, check for the length.
- Then, check if the starting character is ‘#’ using the charAt() method.
- Now, if the above condition is satisfied then, check for the below condition
- Now, iterate over the string s2 and check if the string matches the regex pattern. If the pattern is matched then, return the boolean value true,
- Otherwise, return false.
Regex Pattern:
[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}
package com.company; import java.util.Scanner; public class ColorCode { public static void main(String[] args) { Scanner s = new Scanner(System.in); String s1 = s.next(); boolean b = colorCodeValidation(s1); if (b == true) System.out.println("Valid"); else System.out.println("Invalid"); } public static boolean colorCodeValidation(String s1) { boolean b = false, b1 = false; String s2 = s1.substring(1, s1.length()); if (s1.length() == 7) if (s1.charAt(0) == '#') b1 = true; if (b1 == true) for (int i = 0; i < s2.length(); i++) { char c = s2.charAt(i); if (c != '#') { if (s2.matches("[A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}")) b = true; else { b = false; break; } } } return b; } }
Output
Print Color Name using Color Code(VIBGYOR)
Write a code to display the color names using the VIBGYOR pattern using a switch case. For example, if the user enters color code O then the program should return Orange.
Input and Output Format
- Input consists of a character.
- The output consists of a string.
Refer sample output for formatting specifications
Sample Input 1:
Y
Sample Output 1:
Yellow
Program to display Color Name using Switch Case
Here, we need to just pass the character to the switch case and check for each input. And, the program will return the appropriate color name.
package com.company; import java.util.Scanner; public class ColorCode { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); char c = scanner.next().charAt(0); System.out.println(vibgyorColorCodes(c)); } private static String vibgyorColorCodes(char c) { switch (c) { case 'V': return "Violet"; case 'I': return "Indigo"; case 'B': return "Blue"; case 'G': return "Green"; case 'Y': return "Yellow"; case 'O': return "Orange"; case 'R': return "Red"; } return "Invalid"; } }
Output
Thus, in this way, we learn how to validate color code in Java. Also, saw how to find the color name using color code.