Whenever i try to compile this function it gives an error at line 10 --> ErrorMessage : CandidateCode.java.10: illegal start of type static HashMap hm = new HashMap<>(); 1 error I'm trying to compile it on an website's compiler, but when i use netbeans it works completely fine.
import java.util.*;
public class CandidateCode {
static int rep, total = 0, sum = 0, i = 0, j = 0;
static HashMap<Integer, Integer> hm = new HashMap<>();
static ArrayList<Integer> al;
public static int DistributingMedals(int input1, int[] input2, int[] input3,
int[] input4, int input5) {
//Write code here
for (i = 0; i < input1; i++) {
int start = input3[i];
int end = input4[i];
int count = input2[i];
for (j = start; j <= end; j++) {
try {
sum = hm.get(j);
} catch (Exception e) {
e.getMessage();
sum = 0;
}
sum = sum + count;
hm.put(j, sum);
}
}
int chk = 0;
Collection<Integer> valcol = hm.values();
casper:
while (chk < valcol.size()) {
for (int max : valcol) {
total = max + total;
if (total > input5) {
al = new ArrayList(hm.keySet());
Object obj = al.get(chk);
rep = (Integer) obj;
break casper;
}
chk++;
}
}
return rep;
}
}