gpt4 book ai didi

java - 如何将字符串拆分为列

转载 作者:太空宇宙 更新时间:2023-11-04 07:51:23 25 4
gpt4 key购买 nike

我有一个单列字符串列表。我想从这些字符串中创建三列,然后将输出打印到另一个文件。我该怎么做?

这是我迄今为止尝试过的:

ArrayList<String> list=new ArrayList<String>();
File file = new File("f://file.txt");

try {
Scanner scanner = new Scanner(file);

while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}

scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}

我的输入数据:

City
Gsk
Relocation
sripallu
here
jrajesh
gurgaon
unitech
StatisticsThreads
WizOty
LTDParsvnathK
Quotesby
newest
PMaashuktr

我的预期输出:

City      Gsk      Relocation
sripallu here jrajesh
gurgaon unitech StatisticsThreads
WizOty LTDParsvnathK Quotesby
newest PMaashuktr Loans
.
.
.
.
.
.
.

最佳答案

您可以在类里面构建您的需求,例如输出,并列出输出列表。

public class Output{
private String str1;
private String str2;
private String str3;
<geter & setter method>
}

...

ArrayList<Output> list=new ArrayList<Output>();
int i=-1; Output op =null;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();i = ++i%3;
if(i==0){
op = new Output();
op.setStr1(line);
}else if(i==1)
op.setStr2(line);
else
op.setStr3(line);
}

关于java - 如何将字符串拆分为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14413750/

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