Je passe une chaîne dans ma méthode de parser de chanson et elle échoue et je n'arrive pas à comprendre pourquoi. Tout est de retour nul ou 0.Pourquoi mon matcher échoue-t-il?
Ma méthode d'analyse syntaxique est
public static Song parseSong(String songString){
Map<String, String> songMap = new HashMap<String, String>();
Pattern pattern = Pattern.compile(".*<key>(.+)</key><(.+)>(.+)</.+>.*\n");
Scanner scanner = new Scanner(songString);
if(scanner.hasNext(pattern))
{
String line = scanner.next(pattern);
Matcher matcher = pattern.matcher(line);
MatchResult result = matcher.toMatchResult();
songMap.put(result.group(1), result.group(3));
}
int count = 0, rating = 0;
try{
count = Integer.parseInt(songMap.get("Play Count"));
}
catch(Exception e)
{
//bury this for now will handle when rest is working
}
try{
rating = Integer.parseInt(songMap.get("Rating"));
}
catch(Exception e)
{
//bury this for now will handle when rest is working
}
return new Song(songMap.get("Name"), songMap.get("Artist"), songMap.get("Album"),
songMap.get("Genre"), count, rating, songMap.get("Location"));
}
String songString = "<key>Track ID</key><integer>160</integer>\n"+
"<key>Name</key><string>Ashley</string>\n"+
" <key>Artist</key><string>Escape the Fate</string>\n"+
" <key>Composer</key><string>Luca Gusella</string>\n"+
" <key>Album</key><string>This War Is Ours</string>\n"+
" <key>Genre</key><string>Metal</string>\n"+
"<key>Kind</key><string>AAC audio file</string>\n"+
" <key>Size</key><integer>7968219</integer>\n"+
" <key>Total Time</key><integer>246503</integer>\n"+
" <key>Track Number</key><integer>17</integer>\n"+
" <key>Year</key><integer>2005</integer>\n"+
" <key>Date Modified</key><date>2009-07-27T01:17:29Z</date>\n"+
" <key>Date Added</key><date>2009-07-27T01:17:00Z</date>\n"+
"<key>Play Count</key><integer>150</integer>\n"+
" <key>Bit Rate</key><integer>256</integer>\n"+
" <key>Sample Rate</key><integer>44100</integer>\n"+
" <key>Comments</key><string>\"Amanda\" performed by Aisha Duo from the CD Quiet Songs, courtesy of Obliq Sound. Written by Luca Gusella, published by Editions ObliqMusic (GEMA). All Rights Reserved. Used by Permission. </string>\n"+
" <key>Skip Count</key><integer>1</integer>\n"+
" <key>Skip Date</key><date>2009-07-27T01:46:32Z</date>\n"+
" <key>Artwork Count</key><integer>1</integer>\n"+
" <key>Persistent ID</key><string>A4D6F35FE9F41B58</string>\n"+
" <key>Track Type</key><string>File</string>\n"+
" <key>Location</key><string>file://localhost/C:/Documents%20and%20Settings/MB24244/Desktop/music/07%20Knees.m4a</string>\n"+
"<key>File Folder Count</key><integer>4</integer>\n"+
"afgjdhfshsgsughghanoise\n"+
"<key>Library Folder Count</key><integer>1</integer>\n"+
"<key>Rating</key><integer>100</integer>";
Quelqu'un peut-il aider à expliquer ce qui ne va pas avec mon approche et pourquoi les groupes ne fonctionnent pas (qui est ce qui semble être le problème)
Eh bien, j'écris l'application c'est comme un exercice d'entraînement et j'espérais apprendre un peu sur l'analyse que je veux dire Il était facile d'en arriver là. J'essaye d'analyser une bibliothèque de musique d'itunes.xml pour un projet de compagnie. Si je ne peux pas comprendre, je vais passer à quelqu'un d'autre a déjà écrit. –
J'ai fait quelques petites modifications pour obtenir une implémentation fonctionnelle de votre code. – pjp