Scanner sc1 = new Scanner(System.in);
String[] s1 = sc.nextLine().split("\\)|\\(|,");
int x1 = Integer.parseInt(s1[0]);
int y1 = Integer.parseInt(s1[1]);
int x2 = Integer.parseInt(s1[2]);
int y2 = Integer.parseInt(s1[3]);
输入和报错如下:
Scanner sc = new Scanner(System.in);
String[] s1 = sc.nextLine().replaceAll("\\)\\(",",").replaceAll("\\)|\\(","")
.split(",");
println(s1.toString())
int x1 = Integer.parseInt(s1[0]);
int y1 = Integer.parseInt(s1[1]);
int x2 = Integer.parseInt(s1[2]);
int y2 = Integer.parseInt(s1[3]);
您可以先对里面的特殊字符处理之后再进行分割,应该会直观和简单很多
你的输入是:(1,2,3,4)?那就写成String[] s1 = sc1.nextLine().replace("(", "").replace(")", "").split(",");
你都没有看拆分完是什么样的么,根据你写的括号逗号去拆分,那结果就是 [, 2, 1, , 3, 4] 长度为6,存在空的数据,直接parseInt肯定会出错呀