-3

If this is the given input:

An Uncensored Input:

Name: John Doe

Email: john@school.edu

Phone: 456-832-7180 

This is what I want:

The newly Censored input:

Name: J*** **e

Email: j***@s*****.edu

Phone: XXX-XXX-7180

These are the instructions given to me: When censoring information, we will be using the following format:

“Type: Information_here\n”

○ Type we want to filter can be “Name”, “Email” or “Phone”

○ There will always be a colon and a space between the “Type” and “Information_here”

○ Each line will end with a newline character (\n)

I tried using array list and substrings in order to isolate first and last name but somehow could not do it. The problem is that the user could enter either just name or just phone or all three or any two. That has made this very annoying. My if statements don't work even though I am able to censor it.

this is where i have gotten:

            System.out.println("Please enter the phrase you would like to censor information from: ");

            while (true) {

                // Obtain a line from the user
                String temp = scanner.nextLine();

                if (!passage.isEmpty() && temp.isEmpty()) {
                    break;
                } else if (passage.isEmpty() && temp.isEmpty()) {
                    continue;
                }


                // Add the contents of temp into the phrase
                passage += temp;


                // Append a newline character to each line for parsing
                // This will separate each line the user enters
                // To understand how input is formatted in Part 3, please refer to the handout.
                passage += '\n';

            }

            // Print the uncensored passage
            System.out.println("Uncensored: ");
            System.out.println(passage);

            ArrayList<String> obj = new ArrayList<String>();
            String s[] = passage.split("/n");
            obj.addAll(Arrays.asList(s));

            String what = obj.get(0);
            String cens = "";
            if (what.substring(0, 1) == "N"){
                String whole = what.substring(6,what.length());
                int space = whole.indexOf(" ");
                String first = whole.substring(0,space);
                String last = whole.substring(space, whole.length());
                String fcen = first.substring(0,1);
                for (int i = 1; i < first.length(); i++){
                    fcen += "*";
                    cens += fcen;
                }
TM00
  • 1,111
  • 1
  • 9
  • 22
  • try posting the relevant parts of your code – Scary Wombat Feb 16 '18 at 06:06
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. Use the [edit] link to improve your *question* - do not add more information via comments. Thanks! – GhostCat Feb 16 '18 at 06:13
  • `what.substring(0, 1) == "N"` - how to compare Strings in Java – Scary Wombat Feb 16 '18 at 06:43
  • `passage.split("/n");` - is this correct? – Scary Wombat Feb 16 '18 at 06:44
  • `String what = obj.get(0);` will get the same String each time – Scary Wombat Feb 16 '18 at 06:45

2 Answers2

1

I took your code as a starting point and created the following:

public static String censorName(String name) {

        // check validity
        if(!name.contains(" ")) {
            return "Invalid entry";
        }

        String censored = "";

        for (int i = 0; i < name.length(); i++) {
            if(i==0 || i== name.length()-1 || name.charAt(i)==' ') {
                censored+= name.charAt(i);
            }else {
                censored += "*";
            }
        }
        return censored;
    }

    public static String censorEmail(String email) {

        // check validity
        if(!email.contains("@")) {
            return "Invalid entry";
        }

        String censored = "";

        for (int i = 0; i < email.length(); i++) {
            if(i==0 || i >= email.indexOf(".") || email.charAt(i)=='@' || email.charAt(i-1)=='@') {
                censored+= email.charAt(i);
            }else {
                censored += "*";
            }
        }
        return censored;
    }

    public static String censorPhone(String phone) {

        // check validity
        if(!phone.contains("-")) {
            return "Invalid entry";
        }

        String censored = "";

        for (int i = 0; i < phone.length(); i++) {
            if(i > phone.lastIndexOf("-") || phone.charAt(i)=='-') {
                censored+= phone.charAt(i);
            }else {
                censored += "X";
            }
        }
        return censored;
    }

    public static void main(String[] args) {

        // place your code here...
        String passage  ="Name: John Doe\n" + 
                "Email: john@school.edu\n" + 
                "Phone: 456-832-7180";

        // Print the uncensored passage
        System.out.println("Uncensored: ");
        System.out.println(passage);
        System.out.println();
        ArrayList<String> obj = new ArrayList<String>();
        String s[] = passage.split("\n"); // was wrong way
        obj.addAll(Arrays.asList(s));


        String name  = "", email="", phone="";
        for (String what : obj) {
            if (what.charAt(0) == 'N'){
                // censor name
                String uncensored  = what.substring(6,what.length());
                name = censorName(uncensored);
            }else if(what.charAt(0) == 'E') {

                String uncensored  = what.substring(7,what.length());
                email = censorEmail(uncensored);

            }else if(what.charAt(0) == 'P') {

                String uncensored  = what.substring(7,what.length());
                phone = censorPhone(uncensored);

            }
        }

        String censored  ="Name: "+name+"\n" + 
                "Email: "+email+"\n" + 
                "Phone: "+phone+"\n";

        System.out.println("Censored \n"+censored);


    } 

Output:

Uncensored: 
Name: John Doe
Email: john@school.edu
Phone: 456-832-7180

Censored 
Name: J*** **e
Email: j***@s*****.edu
Phone: XXX-XXX-7180

I noticed your slash was the wrong way when you split the string, it should be "\n". Also, I found it helpful to split the censoring into separate static methods, but you can place that code inside the if-statements as well if you need to.

Hope this helps.

TM00
  • 1,111
  • 1
  • 9
  • 22
-1

You need to check for Type and validate the strings before censoring the data (mask).

For e-mail you can validate through, (from What is the best Java email address validation method?)

public static boolean isValidEmailAddress(String email) {
   boolean result = true;
   try {
      InternetAddress emailAddr = new InternetAddress(email);
      emailAddr.validate();
   } catch (AddressException ex) {
      result = false;
   }
   return result;
}

For Phone No: Matching with a regex string will do,

 Pattern p = Pattern.compile("^[0-9\\-]*$");
 Matcher m = p.matcher("111-444-5555");
 boolean b = m.matches();

Similarly for Name:

Pattern p = Pattern.compile("^[\\p{L} .'-]+$");
Matcher m = p.matcher("Patrick O'Brian");
boolean b = m.matches();

Regex is from Java Regex to Validate Full Name allow only Spaces and Letters