Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

use DRJAVA and make sure it is work Assignment 5 - Wild Card In computers the %

ID: 3934296 • Letter: U

Question

use DRJAVA and make sure it is work

Assignment 5 - Wild Card

In computers the % character is sometimes used to represent wildcard characters that may be substituted for any set of characters. For instance, if you search a database of first and last names for "% Jones" your computer returns a list of all names in the database with the last name Jones.

For this assignment you will input a String that contains a single % character, then a second String. The % will be replaced by the second String. So for example, if the user enters the Strings "d%g" and "in", the program outputs ding.

The original String must contain only letters of the alphabet, capital or lowercase, spaces and tab, and a single %. Any additional %'s can be treated as an invalid characters. The replacement String may be any legal String in Java.

If the first String does not contain a % "Error: no %" should be output.

If the first String contains anything other than letters of the alphabet, spaces or tabs then "Error: Incorrect characters" should be printed. If the first String does not have a % you do not have to check for incorrect characters, only "Error: no %" should be output.

Sample Run 1:

Enter the first String:
D%g
Enter the replacement String:
in
Ding

Sample Run 2

Enter the first String:
$Wild%$
Enter the replacement String:
Card
Error: Incorrect characters

NOTE: You MUST use the class name "Main" for this assignment. REMEMBER: you must SUBMIT your answer. Your assignment doesn't count as complete unless it has been submitted.

Explanation / Answer

CODE:

import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
       String s1,s2,choice;
       Scanner sc= new Scanner(System.in);
       do{

           System.out.println("Enter First String:");
           s1=sc.nextLine();
           System.out.println("Enter replacement String:");
           s2=sc.nextLine();

           //matches if string 1 contains only alphabets,spaces, tabs and a single %
           if(s1.matches("[a-zA-Z\s\t]+%[a-zA-Z\s\t]+")){ //(a-zA-Zs )%(a-zA-Zs )
               System.out.println(s1.replace("%", s2));
           }else{
               if(!s1.contains("%")){
                   System.out.println("Error: no %");
               }else{
                   System.out.println("Error: Incorrect characters");
               }
           }

           System.out.println(" Do you want to try again:[y/n]");
           choice=sc.nextLine();
       }while(choice.equalsIgnoreCase("y"));
       sc.close();
   }

}

OUPUT:

Enter First String:
d%e
Enter replacement String:
in
dine

Do you want to try again:[y/n]
y
Enter First String:
$W%d
Enter replacement String:
il
Error: Incorrect characters

Do you want to try again:[y/n]
y
Enter First String:
S%r%g
Enter replacement String:
in
Error: Incorrect characters

Do you want to try again:[y/n]
n