So I've created a simple program that asks a user to enter 5 genres and then score them out of 10. I've not added any validation, but I'm not worried about that. As you can see, I have two arrays: genres[] and score[]. Let's say they enter:
[1] : Genre A | 3
[2] : Genre B | 6
[3] : Genre C | 2
[4] : Genre D | 10
[5] : Genre E | 8
The results should be listed Genre D, E, B, A, C
Here's my overall code:
import java.util.Scanner;
class OrigClass {
public static void main (String[] args){
Scanner ScanObj = new Scanner(System.in);
int count;
String[] genres;
genres = new String[5];
int[] score;
score = new int[5];
for (count = 0; count < 5;count++){
System.out.println("Enter A Genre: ");
genres[count] = ScanObj.nextLine();
System.out.println("How much do you like it? ");
score[count] = ScanObj.nextInt();
ScanObj.nextLine();
}
for (count = 0; count < 5; count++){
System.out.println(count+1 + ") " + genres[count] + " " + score[count] + "/10");
}
ScanObj.close();
}
}
Is there a clever way to do it in java using some functions or would I have to manually do it by using temporary varibales, if statements etc. I suppose I could also use the bubble sort algorithm which would be fairly easy to implement. So I want to sort the contents of score[] into descending order. Any tips would be appreciated.
Map? With aMapit would be way easier; swallow theSet<Entry<K, V>>into aListand useCollections.sort()with a customComparator.