I am passing a string into my song parser method and it is failing and I can't figure out why. Every thing is returning null or 0.
My parser method is
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>";
Can anyone help explain what is wrong with my approach and why the groups aren't working (which is what appears to be the problem)