简体   繁体   中英

How do I use the value of a variable AS a variable

Haven't come across this in coding for the android yet. So how do I use the value of one variable as a new variable.

I want to take the value of a variable like "file1.mp3" strip off the extension and then append text to the variable and use it as the variable name like file1_title.txt and file1_desc.txt.


so fName[1] might equal file1.mp3

and then I want to create to new variable

file1_title.txt equals "Song Title One"

file1_desc.txt equals "Description of file One"

both based on the value of fname[1]


fName[2] equals file2.mp3

file2_title.txt equals "Song Title Two"

file2_desc.txt equals "Description of file Two"

both based on of the value fName[2]


etc...

How is this done for the android

I'm not 100% sure I understand the details of your questions, but use a Map. The "key" would be the song title, the value would be the description.

Some followup. Lots of handwaving, no error checking. Assumes that an mp3 File is coming in, and somehow you read the title and description from the tags in the MP3 file. YMMV

// TreeMap will sort by titles which seems reasonable
Map<String, String> songMapTitleToDesc = new TreeMap<String, String>();

MyMP3Reader mmp3r = new MyMP3Reader(File inFile);
String songTitle = mmp3r.getSongTitle();
String songDesc = mmp3r.getSongDesc();
songMapTitleToDesc.put(songTitle, songDesc);
mmp3r.close();  // or whatever

Not sure if this is what you're looking for. It is basic Java string formating.

String attr1 = "song.mp3";
String attr2 = attr1.split(".")[0] + ".txt";

Naturally add the necessary null checks.

==UPDATE==

So if I understand you correctly, you get a file name ("asd.mp3") and need the song title and its description.

String attr1 = "song.mp3";
String songname = "";
String songdesc = "";
String[] splitArray = attr1.split(".");
if(splitArray[0] != null){
    String songname = attr1.split(".")[0];
    File f = new File(path + songname +".txt");
    //I didn't quite understand in what format you get the data, 
    //especially the  description. However, it could be in a map 
    //where the songname is the key, as suggested above, and here you would write that description to file(f)

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM