用多个分隔符检查段落的部分。(JAVA)
如果这是给定的输入: 未经审查的输入:用多个分隔符检查段落的部分。(JAVA),java,arrays,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 以下是给我的指示: 在审查信息时,我们将使用以下格式: “类型:此处的信息\n” ○ 我们要筛选的类型可以是“姓名”、“电子邮件”或“电话”
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
以下是给我的指示:
在审查信息时,我们将使用以下格式:
“类型:此处的信息\n”
○ 我们要筛选的类型可以是“姓名”、“电子邮件”或“电话”
○ “Type”和“Type”之间总是有一个冒号和一个空格
“信息在这里”
○ 每行将以换行符(\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;
}
System.out.println(“请输入您希望从中审查信息的短语:”;
while(true){
//从用户处获取一行
字符串温度=scanner.nextLine();
如果(!passion.isEmpty()&&temp.isEmpty()){
打破
}else if(passion.isEmpty()&temp.isEmpty()){
持续
}
//将temp的内容添加到短语中
通道+=温度;
//将换行符追加到每行以进行分析
//这将分隔用户输入的每一行
//要了解第3部分中输入的格式,请参阅讲义。
段落+='\n';
}
//打印未经审查的段落
System.out.println(“未经审查:”);
系统输出打印LN(通道);
ArrayList obj=新的ArrayList();
字符串s[]=段落.split(“/n”);
obj.addAll(Arrays.asList);
字符串what=obj.get(0);
字符串cens=“”;
如果(什么子字符串(0,1)=“N”){
字符串整数=what.substring(6,what.length());
int-space=整数。indexOf(“”);
String first=整个子字符串(0,空格);
字符串last=whole.substring(空格,whole.length());
字符串fcen=first.substring(0,1);
for(int i=1;i
在审查数据(掩码)之前,您需要检查类型并验证字符串
对于电子邮件,您可以通过(从)进行验证
对于电话号码:匹配正则表达式字符串即可
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();
Regex来自我以您的代码为起点,创建了以下代码:
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);
}
我注意到在拆分字符串时斜杠的方式是错误的,应该是“\n”
。此外,我发现将审查拆分为单独的静态方法很有帮助,但是如果需要,也可以将该代码放在if语句中
希望这有帮助。尝试发布代码问题的相关部分,以寻求调试帮助(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现问题所需的最短代码。没有明确问题说明的问题对其他读者没有用处。请参阅:如何创建问题。使用链接改进问题-不要通过注释添加更多信息。谢谢!
what.substring(0,1)=“N”
-如何比较Java段落中的字符串。拆分(“/N”);
-这是否正确?String what=obj.get(0)
每次都会得到相同的字符串我对java的了解不够,无法理解您在上面所做的事情,但感谢您抽出时间!我不知道为什么有人在没有发表评论的情况下投了反对票。
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