我试过通过搜索类似的代码来解决我的问题,绞尽脑汁,甚至问我的计算机科学老师但是没有用。这是我正在为学校制作的音乐数据库程序的一部分,目前我在设置主类中的指针等于另一个类中的指针时遇到了问题。
以下是我尝试在代码中查找错误时打印出来的代码示例:
应该打印出来的内容:
Title: Numb
Artist: Linkin Park
Album: Meteora
Genre: Rock
Title: Empty
Artist: JYJ
Album: The Beginning
Genre: Pop
Title: Teardrops On My Guitar
Artist: Taylor Swift
Album: N/A
Genre: Country
我的显示方法打印出来的内容:
DISPLAY
Title: Teardrops On My Guitar
Artist: Taylor Swift
Album: N/A
Genre: Country
Title: Teardrops On My Guitar
Artist: Taylor Swift
Album: N/A
Genre: Country
Title: Teardrops On My Guitar
Artist: Taylor Swift
Album: N/A
Genre: Country
正如您所看到的,它确实打印出三个条目,但打印出最后一个条目而不是每个条目。
以下是我认为是我的问题的罪魁祸首的代码部分,虽然我不确定如何解决它们:
Node first = null;
int lines = edit.linesInFile();
for (int i = 0; i < lines; i++){
first = edit.setNode(first, i);
System.out.println(first.getData());
}
display.oneAtATime(first);
for循环中的打印行显示了文本文件中的输出,所以我知道问题不在于将数据放在主类中,或者在循环中首先设置等于它。 但是,显示不会显示与循环中的print语句相同的输出。
这是我的显示类中的oneAtATime方法:
public void oneAtATime(Node ptr){
System.out.println("DISPLAY");
while(ptr != null){
System.out.println(ptr.getData());
ptr = ptr.getNext();
}
}
ptr只是我的方法返回的一个指针,它将节点的数据设置为包含歌曲标题,艺术家等的音乐对象。
这是我的第一篇文章,我不确定我应该包括哪些内容。如果需要其他任何东西,请随时告诉我。提前谢谢!
编辑:好的,这是我到目前为止使用的所有代码。请记住,很多工作正在进行中。这是我的主要类,名为MusicMain.java:
package components;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import java.util.*;
public class MusicMain extends JPanel implements ActionListener {
static JTextField itemName = new JTextField(10);
static JTextField width = new JTextField(10);
static String radioText = "";
static JCheckBoxMenuItem yes;
Display display = new Display();
Edit edit = new Edit();
public MusicMain (){
JButton mLButton = new JButton("Music List");
mLButton.getPreferredSize();
add(mLButton);
mLButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
display.musicList();
}
});
JButton addButton = new JButton("Add");
addButton.getPreferredSize();
add(addButton);
addButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
edit.add();
}
});
JButton sortButton = new JButton("Sort");
sortButton.getPreferredSize();
add(sortButton);
sortButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
display.musicList();
}
});
}
public void actionPerformed(ActionEvent arg0) {
}
private static void createAndShowGUI() {
JMenuItem menuItem;
JMenu submenu;
//Create and set up the window.
JFrame frame = new JFrame("Music Database");
frame.setLayout(null);
frame.setIconImage(new ImageIcon("fire.jpg").getImage());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JMenuBar grayMenuBar = new JMenuBar();
JPanel unitsPanel = new JPanel();
unitsPanel.setOpaque(true);
unitsPanel.setBackground(new Color(128, 158, 182));
unitsPanel.setPreferredSize(new Dimension(200, 20));
//Build the first menu.
JMenu fileMenu = new JMenu("File");
fileMenu.setMnemonic(KeyEvent.VK_F);
grayMenuBar.add(fileMenu);
grayMenuBar.setOpaque(true);
grayMenuBar.setBackground(new Color(128, 158, 182));
grayMenuBar.setPreferredSize(new Dimension(200, 20));
JMenu newSubmenu = new JMenu("New");
newSubmenu.setMnemonic(KeyEvent.VK_N);
fileMenu.add(newSubmenu);
JMenuItem newSong = new JMenuItem("Song");
newSubmenu.add(newSong);
JMenuItem newDB = new JMenuItem("Database");
newSubmenu.add(newDB);
fileMenu.add(newSubmenu);
JMenuItem loadDatabase = new JMenuItem("Load", KeyEvent.VK_L);
fileMenu.add(loadDatabase);
loadDatabase.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
}
});
JMenuItem exit = new JMenuItem("Exit", KeyEvent.VK_E);
fileMenu.add(exit);
exit.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
JPanel panel = new JPanel();
JLabel verify = new JLabel("Are you sure you would like to exit?");
verify.setSize(120, 30);
panel.add(verify);
yes = new JCheckBoxMenuItem("Yes");
panel.add(yes);
JOptionPane.showOptionDialog(null, panel,
"Verification", JOptionPane.OK_CANCEL_OPTION,
JOptionPane.PLAIN_MESSAGE, null, null, null);
if (yes.isSelected()){
System.exit(0);
}
}
});
frame.setJMenuBar(grayMenuBar);
MusicMain newContentPane = new MusicMain();
newContentPane.setOpaque(true);
frame.setContentPane(newContentPane);
//Display the window.
frame.setSize(200, 300);
frame.setVisible(true);
}
public static void main (String[] args){
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run(){
Edit edit = new Edit();
Info info = new Info();
Display display = new Display();
Node first = null;
int lines = edit.linesInFile();
for (int i = 0; i < lines; i++){
first = edit.setNode(first, i);
System.out.println(first.getData());
}
display.oneAtATime(first);
createAndShowGUI();
}
});
}
}
这是我的类名为Display.java的代码:
package components;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Display{
public void musicList(){
JFrame frame = new JFrame("Music List");
frame.setSize(615,400);
frame.setVisible(true);
frame.setLayout(null);
JLabel title = new JLabel("Song Title");
JLabel artist = new JLabel("Artist");
JLabel album = new JLabel("Album");
JLabel genre = new JLabel("Genre");
JLabel info = new JLabel("Info");
title.setBounds(3,0,100,20);
artist.setBounds(169,0,100,20);
album.setBounds(281,0,100,20);
genre.setBounds(449,0,100,20);
info.setBounds(562,0,100,20);
frame.add(title);
frame.add(artist);
frame.add(album);
frame.add(genre);
frame.add(info);
JPanel mL = new JPanel(new GridBagLayout());
GridBagConstraints d = new GridBagConstraints();
JTextArea textArea = new JTextArea(0, 0);
textArea.setFont(new java.awt.Font("Courier New", 0, 12));
textArea.setEditable(false);
JPanel boxes = new JPanel(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
try{
int count = 0;
File file = new File("musiclist.txt");
String text = new Scanner(file).useDelimiter("\\A").next();
StringTokenizer tokens = new StringTokenizer(text);
while (tokens.hasMoreTokens()){
String token = tokens.nextToken(",");
count++;
int x = token.length();
if (((count % 4) == 1) || ((count % 4) == 3)){
if (x < 8){
textArea.append(token + " ");
}
else if (x < 16){
textArea.append(token + " ");
}
else{
textArea.append(token + " ");
}
}
else{
if (x < 8){
textArea.append(token + " ");
}
else{
textArea.append(token + " ");
}
}
}
for (int i = 0; i < (count / 4); i++){
JButton button = new JButton();
button.setPreferredSize(new Dimension(22, 14));
c.gridy = i;
boxes.add(button, c);
}
d.weighty = 1;
d.weightx = 1;
d.gridx = 0;
d.anchor = GridBagConstraints.NORTHWEST;
d.fill = GridBagConstraints.HORIZONTAL;
JScrollPane scroll = new JScrollPane(mL);
scroll.setBounds(0,20,600,343);
mL.add(textArea, d);
c.gridy = 0;
c.anchor = GridBagConstraints.NORTHEAST;
mL.add(boxes, c);
scroll.setViewportView(mL);
frame.add(scroll);
textArea.requestFocus();
}
catch(Exception e2){
System.out.println(e2);
}
}
public void oneAtATime(Node ptr){
System.out.println("DISPLAY");
while(ptr != null){
System.out.println(ptr.getData());
ptr = ptr.getNext();
}
}
}
这是我的类Edit.java的代码:
package components;
import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Edit{
Node node = new Node();
Info info = new Info();
static JTextField titleField = new JTextField(20);
static JTextField artistField = new JTextField(20);
static JTextField albumField = new JTextField(20);
static JTextField genreField = new JTextField(20);
public Info setMusic(int d){
try{
FileReader in = new FileReader("musiclist.txt");
BufferedReader br = new BufferedReader(in);
String text = br.readLine();
for (int i = 0; i < d; i++){
text = br.readLine();
}
StringTokenizer tokens = new StringTokenizer(text);
int count = 0;
while (tokens.hasMoreTokens()){
while (count < 4){
String token = tokens.nextToken(",");
count++;
if (count == 1){
info.setTitle(token);
}
if (count == 2){
info.setArtist(token);
}
if (count == 3){
info.setAlbum(token);
}
if (count == 4){
info.setGenre(token);
}
}
}
}
catch(Exception e2){
System.out.println(e2);
}
return info;
}
public int linesInFile(){
int lines = 0;
try{
FileReader in = new FileReader("musiclist.txt");
BufferedReader bd = new BufferedReader(in);
while ((bd.readLine() != null)){
lines++;
}
}
catch(Exception e2){
System.out.println(e2);
}
return lines;
}
public void add(){
String sTitle = "";
String sArtist = "";
String sAlbum = "";
String sGenre = "";
JFrame addSong = new JFrame("Add Song");
addSong.setSize(200,300);
addSong.setVisible(true);
addSong.setLayout(null);
JLabel title = new JLabel("Song Title");
JLabel artist = new JLabel("Artist");
JLabel album = new JLabel("Album");
JLabel genre = new JLabel("Genre");
title.setBounds(3,0,100,20);
artist.setBounds(3,50,100,20);
album.setBounds(3,100,100,20);
genre.setBounds(3,150,100,20);
titleField.setBounds(3,18,100,20);
artistField.setBounds(3,68,100,20);
albumField.setBounds(3,118,100,20);
genreField.setBounds(3,168,100,20);
addSong.add(title);
addSong.add(artist);
addSong.add(album);
addSong.add(genre);
addSong.add(titleField);
addSong.add(artistField);
addSong.add(albumField);
addSong.add(genreField);
JButton addToList = new JButton("Add");
addToList.getPreferredSize();
addToList.setBounds(3,215, 100, 20);
addSong.add(addToList);
addToList.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
String sTitle = titleField.getText();
String sArtist = artistField.getText();
String sAlbum = albumField.getText();
String sGenre = genreField.getText();
try{
Writer file;
file = new BufferedWriter(new FileWriter("musiclist.txt", true));
file.append(sTitle + "," + sArtist + "," + sAlbum + "," + sGenre + "," + "\n");
file.close();
}
catch(Exception e2){
System.out.println(e2);
}
}
});
}
}
这是我的类Info.java的代码:
package components;
import java.util.*;
import java.io.*;
public class Info{
private String title;
private String artist;
private String album;
private String genre;
public void song(){
String title = "";
String artist = "";
String album = "";
String genre = "";
}
//Set methods
public void setTitle (String token){
title = token;
}
public void setArtist (String token){
artist = token;
}
public void setAlbum (String token){
album = token;
}
public void setGenre (String token){
genre = token;
}
//Get methods
public String getTitle(){
return title;
}
public String getArtist(){
return artist;
}
public String getAlbum(){
return album;
}
public String getGenre(){
return genre;
}
public String toString(){
String str;
str = "Title: " + title + "\n" +
"Artist: " + artist + "\n" +
"Album: " + album + "\n" +
"Genre: " + genre;
return str;
}
}
最后是我的Node.java类的代码:
package components;
public class Node{
public Info data;
public Node next;
public Node previous;
public Node(){
this.data = data;
this.next = next;
this.previous = previous;
}
public Info getData(){
return data;
}
public Node getNext(){
return next;
}
public Node getPrevious(){
return previous;
}
public void setData(Info ob){
data = ob;
}
public void setNext(Node n){
next = n;
}
public void setPrevious(Node p){
previous = p;
}
}
其他一些功能不起作用,但是我终于让数组包含我的文本文件中列出的所有歌曲后,我可以修复它们。
哦,文本文件只是一个名为musiclist.txt的同一目录中的文本文件,每首歌都在一个单独的行中,格式如下:
title1,artist1,album1,genre1,
title2,artist2,album2,genre2,
尝试使用ArrayList:
ArrayList<Info> list = new ArrayList<Info>();
for(int i = 0; i < edit.linesInFile(); i++){
list.add(edit.setMusic(i));
System.out.println(list.get(i));
}
System.out.println("DISPLAY");
for(Info n: list){
System.out.println(n.toString());
}
答案 0 :(得分:0)
这类似于您应该使用的功能类型。你的节点方法更多地是用C ++指针构思的。
ArrayList<Node> list = new ArrayList<>();
for(int i = 0; i < edit.linesInFile(); i++{
list.add(new Node(edit.getLine());
}
for(Node n: list){
System.out.println(n.toString())
}
return list;
为什么你要像其他人一样将事物存放在其他类中?
但我想你的C ++指针节点类可能是
Node mainNode = new Node();
for(int i = 0; i < edit.linesInFile(); i++{
Node node = new Node()
node.setVars("");
..
node.setPrevious(mainNode);
mainNode.setNext(node);
mainNode = node;
}
PrintF(mainNode)
这条线对我来说似乎很糟糕
first = edit.setNode(first, i);
它不应该返回任何东西,也许它是我所拥有的步骤mainNode = node;
我需要在这个特定的方法中看到,但基本上里面的东西需要做我做的。
答案 1 :(得分:0)
这是你的问题:
BufferedReader br = new BufferedReader(in);
String text = br.readLine();
for (int i = 0; i < d; i++){
text = br.readLine();
}
您正在做的是循环浏览文件,依次将文本变量设置为文件的每一行。循环结束后,您将拥有该文本变量中的最后一行。这就是你重复最后一行的原因。您的标记化和设置info对象上的数据字段必须在for循环中。
我很惊讶你的CS教授没有找到它。 :-)另外你应该在finally块中关闭你的BufferedReader。