为什么我的程序只打印数组的最后一部分?

问题描述 投票:0回答:1

所以我的教授让我们复制这段特定的代码,但我们必须填补某些部分的空白才能使程序运行。我应该使用“Classics.txt”文件创建一个二维数组并打印输出。我的问题是当我运行程序时,它只显示数组的最后一部分。

附言。我对自己输入代码的部分进行了评论。我的教授指示我们不要编辑从他那里复制的代码。

提前致谢。

import java.io.*;
public class CompactDisc {

    public static void main(String[] args)throws IOException {
        FileReader file = new FileReader("Classics.txt");
        BufferedReader input = new BufferedReader(file);
        String title = null;
        String artist = null;
                // CODE I INPUT MYSELF
        int column = 0;
        String cd[][] = new String[6][6];
                // END
        for (int i = 0; i < cd.length; i++) {
                // CODE I INPUT MYSELF
            title = input.readLine();
            artist = input.readLine();
            cd[i][column] = title;
            column++;
            cd[i][column] = artist;
            column = 0;
                // END
        }
        System.out.println("Contents of Classics:");
        for (int i = 0; i < cd.length; i++) {
                // CODE I INPUT MYSELF
            Song track = new Song(title, artist);
            System.out.print(track);
                // END
        }
    }
}

class Song extends CompactDisc{
    private String title; // The song's title
    private String artist; // The song's artist
    /**
    Constructor
    @param title A reference to a String object
    containing the song's title.
    @param artist A reference to a String object
    containing the song's artist.
    */
    public Song(String title, String artist) {
        this.title = title;
        this.artist = artist;
    }
    /**
    The toString method
    @return A String object containing the name
    of the song and the artist.
    */
    public String toString() {
    return title + " by " + artist + "\n";
    }
}

这是“Classics.txt”文件中的内容:

Ode to Joy
Bach
The Sleeping Beauty
Tchaikovsky
Lullaby
Brahms
Canon
Bach
Symphony No. 5
Beethoven
The Blue Danube Waltz
Strauss

这是它给出的输出:

Contents of Classics:
The Blue Danube Waltz by Strauss
The Blue Danube Waltz by Strauss
The Blue Danube Waltz by Strauss
The Blue Danube Waltz by Strauss
The Blue Danube Waltz by Strauss
The Blue Danube Waltz by Strauss

java arrays netbeans filereader
1个回答
0
投票

使用多维

String[]
来存储
Song
(s)对我来说似乎是错误的,我会创建一个
Song[]
。您可以使用
Files.readAllLines(Path, Charset)
一次读取文件的所有行,然后您知道每两行代表一个
Song
,因此循环并填充一个
Song[]
然后打印内容。像,

List<String> al = Files.readAllLines(Path.of("Classics.txt"),
        Charset.forName("UTF8"));
Song[] cd = new Song[al.size() / 2];
int p = 0;
for (int i = 0; i < al.size(); i += 2) {
    cd[p] = new Song(al.get(i), al.get(i + 1));
    p++;
}
System.out.println("Contents of Classics:");
for (Song track : cd) {
    System.out.print(track);
}

当然,您可以潜在地消除数组和第二个循环

List<String> al = Files.readAllLines(Path.of("Classics.txt"),
        Charset.forName("UTF8"));
System.out.println("Contents of Classics:");
for (int i = 0; i < al.size(); i += 2) {
    System.out.println(new Song(al.get(i), al.get(i + 1)));
}
© www.soinside.com 2019 - 2024. All rights reserved.