0

我收到此错误,不知道为什么我的程序基本上是读取 .txt 文件并将其保存到数组中?任何帮助深表感谢。

error message:Exception in thread "main" java.lang.NoClassDefFoundError: song (wrong name: Song)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$100(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

代码:

import java.util.*;
import java.lang.*;
import java.io.*;
import java.nio.charset.*;

class Song {
    private int songId; // The unique song identifier
    private String title; // The song title
    private String artist; // The song artist
    private String genre; // The genre of the song
    private String album; // The album name
    private String songData;

    public Song(int songId, String title, String artist, String genre,
            String album, String songData) {
        this.songId = songId;
        this.title = title;
        this.artist = artist;
        this.genre = genre;
        this.album = album;
        this.songData = songData;
    }

    public String toString() {
        return "Id: " + songId + ", Title: " + title + ", Artist: " + artist +
            ", Genre: " + genre + ", Album: " + album + ", Data: " + songData;
    }
}

class Ideone {
    public static void main (String[] args) throws java.lang.Exception {
          try {
                FileInputStream fstream = new FileInputStream("songCollection.txt");
                // use DataInputStream to read binary NOT text
                // DataInputStream in = new DataInputStream(fstream);
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;

                while ((strLine = br.readLine()) != null) {
                    String[] splitOut = strLine.split(", ");
                    for (String token : splitOut)
                        System.out.println(token);
                }
                in.close();
            } catch (Exception e) {
                System.err.println("Error: " + e.getMessage());
            }

          Readable fileSong;
            String[] songData = new Scanner(fileSong);

            while (songData.hasNextLine()) {
                String songCollection = songData.nextLine();
                songData = songCollection.split(",");

        List<Song> songs = new ArrayList<>();

        try (BufferedReader input = new BufferedReader(new InputStreamReader(
                System.in, Charset.forName("UTF-8")))) {
            String line;
            while ((line = input.readLine()) != null) {
                String[] arr = line.split(",");
                songs.add(new Song(Integer.parseInt(arr[0]), arr[1], arr[2],
                        arr[3], arr[4], arr[5]));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (Song song : songs) {
            System.out.println(song.toString());
        }
    }
}
4

2 回答 2

0

问题正在发生,因为文件存储为 song.java 而不是Song.java [注意类名中的大写 S]

其次,确保你有一个主类,以防你使用命令调用程序

爪哇之歌

否则,您也可能会收到以下错误。线程“main”中的异常 java.lang.NoSuchMethodError: main

于 2013-10-25T05:20:28.090 回答
-1

我认为你的歌曲类正在抛出异常,因为构造函数没有完全初始化。

因为你的主要方法类给出了这些错误

于 2013-10-25T05:02:14.970 回答