我不知道为什么split命令对我不起作用,sc.nextLine();命令也没有正确读取我的输入,程序输出这些:
Menu:
1 - Sign up on service.
这是我从键盘输入的
1
节目输出:
Input a single line separated by COMMA and NO SPACES, the software will validade your entry.
1 - Your First Name, 2 - Your Second Name, 3 - Your Age, 4 - Your Gender
(F or M in UPPER CASE) 5 - Your Email, 6 - Your Password:
我的第二个输入:
Vanessa,Jhonson,25, M,aaa@aol.com,111222
输入后输出。这是for循环的输出,用于打印String数组k [],不应该接近应该是什么,不知道为什么。
[Ljava.lang.String;@55f96302
Wrong input pattern, try again. //this is the output if the string s doesn't match the regex
Menu: //Program looping (expected)
1 - Sign up on service.
下面这段代码是我的源代码,它有这个主要的方法和另一个来自另一个类的方法,你可以在这之后使用。
package view;
import java.util.Scanner;
import control.RegistrationController;
public class ClientFacade {
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
boolean exit = false;
int option = 0;
RegistrationController rc = new RegistrationController();
while(exit == false){
System.out.println("Menu:");
System.out.println("1 - Sign up on service.");
option = sc.nextInt(); //ERROR AT THIS LINE
switch(option){
case 0:{
exit = true;
break;
}
case 1:{
rc.userSignUp();
break;
}
default:{
System.out.println("Invalid option.");
break;
}
}
}
sc.close();
}
}
以下代码是该程序的方法。 包控制;
import java.util.Scanner;
import java.util.regex.Pattern;
import view.ClientFacade;
import model.Person;
import model.Server;
public class RegistrationController {
public void userSignUp(){
Scanner sc = new Scanner(System.in);
User usr = new User();
RegistrationController rc = new RegistrationController();
String regex = "$(\\w)+(\\,)(\\w)+(\\,)(\\d){2,3}(\\,)[F,M](\\,)(\\w)+(@)(\\w)+(.)(\\w)+((.)(\\w)+)?(,)(\\w)+^";
System.out.println("Input a single line separated by COMMA and NO SPACES, "
+ "the software will validade your entry.\n"
+ "1 - Your First Name, 2 - Your Second Name, "
+ "3 - Your Age, 4 - Your Gender \n(F or M in UPPER CASE) "
+ "5 - Your Email, 6 - Your Password:\n");
String s = sc.nextLine(); //BUG, NOT ABLE TO READ A PROPER STRING
s = s.trim();
String [] k = s.split("(\\,)"); //THIS IS ABSOLUTELY NOT WORKING FOR NO REASON
System.out.println(s); //DEBUGGING LINE
for (int i = 0; i < k.length; i++) { //DEBUGGING BLOCK
String string = k[i];
System.out.println(k);
}
if (Pattern.matches(regex, s)){
usr.setAdmLevel(0);
usr.setName(k[0]+" "+k[1]);
usr.setAge(Integer.parseInt(k[2]));
usr.setGender(k[3]);
usr.setEmail(k[4]);
usr.setPassword(k[5]);
if (rc.registerUser(usr) != 0){
System.out.println("Your are signed up! Your ID: "+usr.getId());
}else {
System.out.println("A problem ocurred, not registered.");
}
}else{
System.out.println("Wrong input pattern, try again.");
}
}
}
答案 0 :(得分:0)
已解决,正则表达式正则表达式启动器和终止符被反转,它应该是"$^"
,它应该是"^$"
,我还发现s.trimm();
不适用于带逗号的完整字符串,所以我在分割后的字符串中修剪它们,在for-each块内。
for (int i = 0; i < k.length; i++) { //TRIMMING BLOCK
k[i] = k[i].trim();
System.out.println(k[i]);
}