Java Questions :Color Code

$ads={1}

Java Questions :Color Code

 

$ads={1}

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
Include a class UserMainCode with a static method validateColorCode which accepts a string. The return type (integer) should return 1 if the color is as per the rules else return -1.
Create a Class Main which would be used to accept a String and call the static method present in UserMainCode.
Input and Output Format:
Input consists of a string.
Output consists of a string (Valid or Invalid).
Refer sample output for formatting specifications.

$ads={1}

Sample Input 1:
#FF9922
Sample Output 1:
Valid
Sample Input 2:
#FF9(22
Sample Output 2:
Invalid

$ads={1}

import java.io.*;
import java.util.*;

public class Main {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s1=sc.next();
if(s1.matches("[#]{1}[A-F0-9]{6}"))
{
System.out.println("Valid");
}
else
{
System.out.println("Invalid");
}

}
}

$ads={1}

Post a Comment

Post a Comment (0)

Previous Post Next Post