英文:
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(" ");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论