-1

this is the class with the variables

package sample;

import java.io.*;

public class Main {
    static String title;
    static String artist;
    static String album;

    public static void main(String[] args) {
        cheese();
        String a=title;
        System.out.println(a);
    }

    public static String cheese(){
        try {
            File song = new File("C:\\Users\\TheFishMoustache\\Music\\Indochine - L'aventurier.mp3");
            FileInputStream file = new FileInputStream(song);
            int size = (int) song.length();
            file.skip(size - 128);
            byte[] last128 = new byte[128];
            file.read(last128);
            String id3 = new String(last128);
            String tag = id3.substring(0, 3);
            if (tag.equals("TAG")) {
                title = id3.substring(3, 32);
                artist = id3.substring(33, 62);
                album = id3.substring(63, 91);
            } else {
                System.out.println(" does not contain information.");
            }
            file.close();
        } catch (Exception e) {
            System.out.println("Error - " + e.toString());
        }
    return (title+album+artist);
    }
}

and in the other class i have this snippet:

@FXML public TableView<Song> musiclist;
private ObservableList<Song> songs() {
    List list = new ArrayList();
    list.add(new Song("a","b","c"));
    ObservableList<Song> info = FXCollections.observableArrayList(list);
    return info;
}

where i want to replace "a","b" and "c" by the "title", "author" and "artist" variables, what code do i need to do this?

Shekhar Rai
  • 2,008
  • 2
  • 22
  • 25
  • 1
    Avoid using `static` variables to pass data. Also since you use the `@FXML` annotation this question may be what you actually need: http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian Mar 01 '17 at 18:16
  • but if i dont, then i cant use the in the cheese() class called in Main() because both are in static – Némo Faucher Mar 01 '17 at 19:04
  • @Némo Faucher, Do you have multiple `main classes`? – Shekhar Rai Mar 02 '17 at 07:00
  • @NémoFaucher You can learn more about OOP and Java, but `new Main().cheese()` will let you call the method when it is not static – OneCricketeer Mar 02 '17 at 07:27

2 Answers2

0

First, you can make a new class.

public class SongLoader {

    private SongLoader() { }

    public static Song fromFile(String fileName) {

        File song = new File(fileName);
        String title, album, artist;

        // Your code here

        return new Song(title, album, artist);
    }
}

And your Main loads that file

public class Main {

    public static void main(String[] args) {
        Song s = SongLoader
            .fromFile("C:\\Users\\TheFishMoustache\\Music\\Indochine - L'aventurier.mp3");

        // For example, print values
        System.out.println(song.title);
    }

where i want to replace "a","b" and "c" by the "title", "author" and "artist" variables

Well, that doesn't make sense.

"a", "b" and "c" are being set into those values of the Song object.

List<Song> list = new ArrayList<>();
list.add(new Song("title","author","artist"));

Or, with my answer

list.add(SongLoader.fromFile("fileName"));
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

The easiest way to accomplish this, and considered best practice, is to use Setters and Getters for your variable references.

In your Main.java, declare the variables as private:

private String title;
private String artist;
private String album;

Also within the Main.java, provide public methods to be used to access those variables. These are called Setters and Getters because they either set, or get, the values from outside that class.

Basic Setter

private void setTitle(String title) {
    this.title = title;
}

Basic Getter

private String getTitle() {
    return this.title;
}

Then, from your other class, you can retrieve those values by simply calling the appropriate getter:

list.add(new Song(
    mainClass.getTitle();
    mainClass.getArtist();
    mainClass.getAlbum();
));

However, I do recommend you do a little more study on the basics of Object Orientated Programming because this concept is an absolute must to master in order to be successful.

Incidentally, IntelliJ can create the setters and getters for you. Just position the cursor in your file where you want them and press Alt-Insert. Again, I recommend understanding how and WHY IntelliJ creates the code it does before relying on it to do so.

Zephyr
  • 9,885
  • 4
  • 28
  • 63