A dooblefleck is a string that conforms to the following format: %u2022it always
ID: 3539919 • Letter: A
Question
A dooblefleck is a string that conforms to the following format:
%u2022it always has 13 characters
%u2022it always has a hash character # as the fourth character
%u2022it always has a minus character - as the eighth character
%u2022the character after the minus character must always be a lowercase letter
%u2022and the last characters of the dooblefleck must be .ed
Write Java code that will obtain a string from the user and then test to see if it is a valid dooblefleck producing appropriate output to the user in either case.
For example the following two strings would be valid doobleflecks BZ8#977-w3.ed 123#hdw-aU.ed
The following would not be a valid dooblefleck d123#hdw-aU.e
Ensure that you write appropriate methods as part of your solution which utilise appropriate parameter passing.
Make sure that you use appropriate user prompts and labels for all output to the screen.
Use Eclipse to create your project and class file. When creating the class use the name that was given above.
Explanation / Answer
Below is the code.
-----------------------------------------------------------------
import java.util.Scanner;
public class Dooblefleck {
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
String input;
boolean isDoobleFleck;
System.out.println("enter the String to check");
input=scan.nextLine();
isDoobleFleck=checkDoubleFleck(input);
if(isDoobleFleck)
System.out.println("Valid dooblefleck");
else
System.out.println("Invalid dooblefleck");
}
private static boolean checkDoubleFleck(String input) {
if(input.length()!=13)
return false;
if(input.charAt(3)!='#')
return false;
if(input.charAt(7)!='-')
return false;
if(Character.isUpperCase(input.charAt(8)))
return false;
if(!input.endsWith(".ed"))
return false;
return true;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.