gpt4 book ai didi

java - 打印从文本文件创建的数组

转载 作者:行者123 更新时间:2023-11-30 02:55:54 24 4
gpt4 key购买 nike

尝试打印包含文本文件中的行的数组。

文本文件:

Queen Greatest_Hits_I Rock 2011 UNI 17 58.19

Madonna Music Pop 2000 Mavericks_Records 10 44.40

Miles_Davies King_of_Blue Jazz 1959 Columbia_Records 6 45.44

Charles_Bradley Changes Soul 2016 Daptone_Records 11 40.44

David_Bowie Blackstar Rock 2016 ISO 7 41.13

Queen Queen_II

Prince Purple_Rain

代码:

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

Scanner read = new Scanner(new File("C:\\CD.txt"));
int a=0,i=0;
while(read.hasNext()){
a++;
read.nextLine();
}read.close();

String [] l = new String [a];
read= new Scanner(new File("C:\\CD.txt"));
while(read.hasNext()){
String n =read.nextLine();
l[i]=n;
i++;
}
System.out.println(Arrays.toString(l));
}

代码输出:

[Queen Greatest_Hits_I Rock 2011 UNI 17 58.19, Madonna Music Pop 2000 Mavericks_Records 10 44.40, Miles_Davies King_of_Blue Jazz 1959 Columbia_Records 6 45.44, Charles_Bradley Changes Soul 2016 Daptone_Records 11 40.44, David_Bowie Blackstar Rock 2016 ISO 7 41.13, Queen Queen_II, Prince Purple_Rain]

我想要的输出:

Queen Greatest_Hits_I Rock 2011 UNI 17 58.19
Madonna Music Pop 2000 Mavericks_Records 10 44.40
Miles_Davies King_of_Blue Jazz 1959 Columbia_Records 6 45.44
Charles_Bradley Changes Soul 2016 Daptone_Records 11 40.44
David_Bowie Blackstar Rock 2016 ISO 7 41.13
Queen Queen_II
Prince Purple_Rain

我做错了什么?

最佳答案

而不是System.out.println(Arrays.toString(l));试试这个:

for (String line : l) {
System.out.println(line);
}

或者不带循环:

System.out.println(StringUtils.join(l, "\n"));

Java 8:

Arrays.stream(l).forEach(System.out::println);

关于java - 打印从文本文件创建的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207883/

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