嗨,我在将string.split值保存到数组时遇到麻烦,在使用以下代码后,数组为空。
Scanner in = new Scanner(System.in);
String input = in.nextLine();
in.close();
if(input.matches("^[0-6][.][0-6]$"))
{
b = false;
String[] coordinates = input.split(".");
int c1 = Integer.parseInt(coordinates[0]);
int c2 = Integer.parseInt(coordinates[1]);
playingfield.PlayTurn(c1, c2);
Run Code Online (Sandbox Code Playgroud)
它告诉我坐标[0]和[1]为空。
到那时为止的所有内容都可以正常运行,并且正则表达式是正确的,因为我可以在那里打印String,并且效果很好
更换
String[] coordinates = input.split(".");
Run Code Online (Sandbox Code Playgroud)
与
String[] coordinates = input.split("\\.");
Run Code Online (Sandbox Code Playgroud)
Split将正则表达式作为参数。.匹配任何内容(意味着整个String),而不是您期望的点字符。
| 归档时间: |
|
| 查看次数: |
67 次 |
| 最近记录: |