gpt4 book ai didi

java - 带空格的分隔符和分隔符后读取空格

转载 作者:行者123 更新时间:2023-12-01 04:52:34 26 4
gpt4 key购买 nike

如何在分隔符(“”)后将空格作为字符串,因为只读取他们的名字。

public class readfile {

public static void main(String[] args) throws IOException {

String readFile = "";
int i;

if (args.length == 1) {
readFile = args[0];

BufferedReader reader = new BufferedReader(new FileReader(readFile));
List<String> read = new ArrayList<String>();
String rLine;
while ((rLine = reader.readLine()) != null) {
String[] items = rLine.split(" ");

if (items[0].equals("Name")) {
for (i = 1; i < items.length; i++) {

String name = items[1];

}

System.out.println("Name is " + items[1]);
}
}
}

}
}


Classlist.txt

Name Alice Mark
Name Rebecca Appel
Name Jonah BullLock Jacob
Name Daniel Ethan Aron

输出:

名字是爱丽丝名字是丽贝卡名字是乔纳名字是丹尼尔

最佳答案

更改以下行:

                       for (i = 1; i < items.length; i++) {

String name = items[1];

}

至:

                  String name = "";
for (i = 1; i < items.length; i++) {

name += items[i];

}

关于java - 带空格的分隔符和分隔符后读取空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14727805/

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