gpt4 book ai didi

java - 用逗号和空格分割输入的字符串

转载 作者:行者123 更新时间:2023-12-01 13:03:29 25 4
gpt4 key购买 nike

        while(scan.hasNext()){
String line = scan.next();
String[] tempArray = line.split(",\\s*");

for (int i=0; i<3; i++){
System.out.println(tempArray[i]);
}

我的输入文件如下所示:

A, 0, 3 
C, 2, 2
BB, 3, 3
DA, -3, 0
ED, 2, -2

它返回 A,然后给我一个错误。给出了什么?

最佳答案

我会用逗号分割,然后用 trim() String,

while(scan.hasNextLine()){                   // <-- hasNextLine()
String line = scan.nextLine(); // <-- nextLine()
String[] tempArray = line.split(","); // <-- split on the comma.

for (int i=0; i<tempArray.length; i++){ // <-- use the array length
System.out.println(tempArray[i].trim()); // <-- trim() the String
}
}

关于java - 用逗号和空格分割输入的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23378533/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com