The problem is that I can't properly display an output.
Input:
6
Berik 78
Zeynep 100
Saniya 75
Daulet 45
Abay 96
Andrey 75
Expected Output:
1) Zeynep: 100
2) Abay: 96
3) Berik: 78
4) Saniya: 75
5) Andrey: 75
6) Daulet: 45
My Output:
1) Zeynep: 100
2) Abay: 96
3) Berik: 78
4) Saniya: 75
4) Andrey: 75
5) Daulet: 45
As you can see, the numbering is incorrect. Bug must be somewhere in incrementation logic when two points are equal. I pass numbering as an argument to the printMax method
import java.util.ArrayList;
import java.util.Scanner;
public class a1_t13 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int num = input.nextInt();
ArrayList<Integer> scores_unsorted = new ArrayList<>();
ArrayList<String> names_unsorted = new ArrayList<>();
for (int i=0; i<num; i++) {
String value = input.next();
int numValue = input.nextInt();
scores_unsorted.add(numValue);
names_unsorted.add(value);
}
int b = 1;
for (int z = 0; z<num;z++) {
int c = getMax(scores_unsorted);
printMax(c, names_unsorted, scores_unsorted, b);
b++; //<================ HERE I'M DOING AN INCREMENTATION
}
}
public static int getMax(ArrayList list) {
int max = 0;
for(int i=0;i<list.size();i++) {
if (max< (int)list.get(i)) {
max = (int) list.get(i);
}
}
return max;
}
public static void printMax(int score, ArrayList names_unsorted, ArrayList scores_unsorted, int order) {
for (int i=0;i<names_unsorted.size();i++) {
if ((int) scores_unsorted.get(i) == score) {
int score_index = scores_unsorted.indexOf(Integer.valueOf(score));
System.out.println(order+")"+names_unsorted.get(score_index).toString()+": "+score);
scores_unsorted.remove(score_index);
names_unsorted.remove(score_index);
}
}
}
}
zwhy not use it? just passz+1to the method