Java String.split()工作不正常,扫描仪读取不正确,出现问题
我不知道为什么split命令对我不起作用,sc.nextLine;命令也没有正确读取我的输入,程序正在输出以下内容:Java String.split()工作不正常,扫描仪读取不正确,出现问题,java,regex,string-split,Java,Regex,String Split,我不知道为什么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 - Yo
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循环打印字符串数组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.");
}
}
}
解决了,正则表达式正则表达式的起始和终止被反转,当它应该是^$时是$^,我还发现s.trimm;对于带有逗号的完整字符串不起作用,所以我在拆分字符串后在每个块的内部对其进行了修剪
for (int i = 0; i < k.length; i++) { //TRIMMING BLOCK
k[i] = k[i].trim();
System.out.println(k[i]);
}
在s.split中不需要\\\\`System.out.printlnk;应该是System.out.printlnk[i]。你在一个数组上调用toString——这从来都不是一个好主意……而且你的正则表达式模式也不符合你的输入。你的输入中还有一个空格,M,它不符合你自己的指令。这一行:s=s.trim;应该消除@user2696372上的空格,但是没有