Question

I've made a simple text box which when a button is pressed loads the text from a text file (line by line). It writes the numbers to a JTextArea then it applies some methods to the numbers and outputs the results into a JTextArea.

My problem is that when the output is shown it repeats the lines - it is easier to show by example.

So if the text file reads:

1
2

The output given is:

First TextArea:

1
2

Second TextArea:

resultFrom1
resultFrom1
resultFrom2

So the numbers are displaying fine but the processed data is repeating itself. In this case it shows the program displaying the first result and then displaying both results.

The code for the load button is:

private void loadPlaylistBtnActionPerformed(java.awt.event.ActionEvent evt) {                                                
    if (evt.getSource() == loadPlaylistBtn) {
        try {
            playlistTextArea.setText(null);
            FileReader fileReader = new FileReader("playlist.txt");
            BufferedReader bufferedReader = new BufferedReader(fileReader);

            String inputFile = "";
            String textFieldReadable = bufferedReader.readLine();

            while (textFieldReadable != null) {
                inputFile += textFieldReadable + "\n";
                textFieldReadable = bufferedReader.readLine();
                trackNum.setText(inputFile);
                String[] loadPlaylist = trackNum.getText().split("\\n");
                for (int i = 0; i < loadPlaylist.length; ++i) {
                    String songName = LibraryData.getName(loadPlaylist[i]);
                    String songArtist = LibraryData.getArtist(loadPlaylist[i]);
                    playlistTextArea.append(songName + " - " + songArtist + "\n");
                }
            }
            fileReader.close();
        } catch (Exception e) {
            System.out.println("File not found.");
        }
    }
} 

The methods in LibraryData are:

getName:

public static String getName(String key) {
    try {
        stmt = con.createStatement();
        ResultSet res = stmt.executeQuery("SELECT * FROM Library WHERE LibraryID = '" +                  
key + "'");
        if (res.next()) { 
            return res.getString(2);
        } else {
            return null;
        }
    } catch (Exception e) {
        return null;
    }
}

and getArtist:

public static String getArtist(String key) {
    try {
        stmt = con.createStatement();
        ResultSet res = stmt.executeQuery("SELECT * FROM Library WHERE LibraryID = '" +     key + "'");
        if (res.next()) {
            return res.getString(3);
        } else {
            return null;
        }
    } catch (Exception e) {
        return null;
    }
}

Any ideas on how I can make it just output one of the songs relating to the number, rather than repeating itself?

Sorry if this has been really unclear - I'm still quite new to Java.

Thanks :)

Was it helpful?

Solution

Using the assumption that playlistTextArea is your problem test box, the problem seems to be:

playlistTextArea.append(songName + " - " + songArtist + "\n");

You're reading the file lines one at a time, and then appending the entire list every single time, which will result in the pattern 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, etc. Easiest way to resolve this would be to make sure you clear the text box before the for loop (i.e. - Move to the playlistTextArea.setText(null) down).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top