英文:
How to read space separated string in one line using Scanner class?
问题
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String s = sc.nextLine();
String[] arr = s.split(" ");
//无法通过空格" "拆分字符串。
英文:
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
String s = sc.nextLine();
String[] arr = s.split(" ");
//not able to split the string by whitespace" ".
答案1
得分: 1
nextLine
在这里首先读取了行的剩余部分根据文档,因此您需要再调用nextLine
。
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine(); // 添加了这一行
String s = sc.nextLine();
String[] arr = s.split(" ");
英文:
nextLine
here reads the rest of the line first according to the docs, so you need another call to nextLine
.
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine(); // added
String s = sc.nextLine();
String[] arr = s.split(" ");
专注分享java语言的经验与见解,让所有开发者获益!
评论