Question: Write a Java program that asks the user to input a string and checks if it's a valid gift card code according to these rules:

Write a Java program that asks the user to input a string and checks if it's a valid gift card code according to these rules:
The gift card code must be exactly 8 characters long.
The first three characters must be uppercase letters.
The fourth character must be a hyphen (-).
The last four characters must be digits.
Your program should use two methods:
isValidGiftCardCode(String code): This method takes a string as input and returns true if the code is valid according to the rules above, and false otherwise. This method should use if conditions and the substring() method to check the gift card code rules. You might also find Character.isUpperCase() and Character.isDigit() helpful.
printResult(boolean isValid): This method takes a boolean value as input. If the input is true, it prints "Valid Gift Card Code". If the input is false, it prints "Invalid Gift Card Code".
Your main program should use a do-while loop to repeatedly ask the user for a gift card code until a valid one is entered. Inside the loop, the program should call the isValidGiftCardCode() method to check the entered code and the printResult() method to display the result.
Example Usage:
Enter a gift card code: ABC-1234
Valid Gift Card Code
Enter a gift card code: abc-1234
Invalid Gift Card Code
Enter a gift card code: ABCD-1234
Invalid Gift Card Code
Enter a gift card code: ABC12345
Invalid Gift Card Code
Write a Java program that asks the user to input

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!