Im quite new to Java and i have stumbled upon the following problem.
Im reading a list of movies from a txt file, one of the fields is a string representation of what genre(s) the movie is classified as, aswell as a numerical representation of 1-5 signifying that the movie has recieved one or more awards.
eks. one movie could have the following value in this field “12bSt”
this would signify that the movie is a b = biographical, S = sports, 2 = won an academy award.
atm i do this:
String[] genreStringToArray(String genre) {
char[] genreCharArray = genre.toCharArray();
this.genreArr = new String[genreCharArray.length];
for (int i = 0; i < genreCharArray.length; i++) {
switch (genreCharArray[i]) {
case 'a': genreArr[i] = "Action"; break;
case 'A': genreArr[i] = "Animation"; break;
case 'b': genreArr[i] = "biographical"; break;
case 'c': genreArr[i] = "comedy"; break;
case 'C': genreArr[i] = "children"; break;
case 'd': genreArr[i] = "drama"; break;
case 'D': genreArr[i] = "documentary"; break;
case 'e': genreArr[i] = "epic"; break;
..... etc
case 2:genreArr[i] = "Academy award"; break;
case 3:genreArr[i] = "Palme d`or"; break;
case 4:genreArr[i] = "Sight & sound"; break;
case 5:genreArr[i] = "AFI top 100"; break;
}
}
return genreArr;
}
my question is, what implementation would be more effective than this?
Use a map with the character (or just a string) as the key, the string as the value.
Essentially the same thing could be done with
enums, and if you need to pass around that info, it might be better to do it in a typesafe way.