Reputation: 51
In my code I need to convert a String to an Integer - the String comes from the extraction of metadata, which is then compared to a array of numbers and their corresponding genres. Unfortunately the string doesn't simply contain numbers, it contains some spaces and brackets, meaning the usual:
String genre = mf.genre(byteArr);
// this gets the metadata from the byte array (It's of no consequence to this but it makes it easier to understand)
int genreInt = Integer.parseInt(genre);
System.out.println(genreInt);
doesn't work, returning
Exception in thread "main" java.lang.NumberFormatException: For input string: " (13)"
As the 13 (for example) is the only part of this I want/need, what would be the easiest way to go about obtaining just this number from the 'messy' string? thanks
Upvotes: 0
Views: 236
Reputation: 8657
You can use the following:
System.out.println(genre.replaceAll("[^0-9]", ""));
Upvotes: 4