通过多个分隔符检查段落的部分。(JAVA)

时间:2018-02-16 06:00:59

标签: java arrays

如果这是给定的输入:

未经审查的输入:

Name: John Doe

Email: john@school.edu

Phone: 456-832-7180 

这就是我想要的:

The newly Censored input:

Name: J*** **e

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

Phone: XXX-XXX-7180

这些是给我的指示: 审查信息时,我们将使用以下格式:

“输入:Information_here \ n”

○我们要过滤的类型可以是“姓名”,“电子邮件”或“电话”

○“Type”和。之间总会有一个冒号和一个空格 “Information_here”

○每一行都以换行符结尾(\ n)

我尝试使用数组列表和子字符串来隔离名字和姓氏,但不知何故不能这样做。 问题是用户可以输入名称或只是电话或全部三个或任意两个。这使得这非常烦人。即使我能够审查它,我的if语句也不起作用。

这是我得到的地方:

            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;
                }

2 个答案:

答案 0 :(得分:1)

我将您的代码作为起点并创建了以下内容:

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);


    } 

输出:

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

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

当你分割字符串时,我注意到你的斜杠是错误的,它应该是"\n"。此外,我发现将审查拆分为单独的静态方法很有帮助,但如果需要,也可以将该代码放在if语句中。

希望这有帮助。

答案 1 :(得分:-1)

在审查数据(掩码)之前,您需要检查Type并验证字符串。

对于电子邮件,您可以通过(来自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;
}

对于电话号码:与正则表达式字符串匹配,

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

同样名称:

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

正则表达式来自Java Regex to Validate Full Name allow only Spaces and Letters