Input : 5 BatsmanA,20 BatsmanB,25 BatsmanC,40 BatsmanD,10 BatsmanE,20
The output must print the Batsman name who scored the largest runs. Here the output must be BatsmanC
public static void main(String[] args) {
//Your Code Here
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int max = 0;
String batsman = "";
for(int i =0; i< N; i++){
String a = sc.nextLine();
String[] n = a.split(",");
int b = Integer.parseInt(n[1]);
if (b > max){
max = b;
batsman = n[0];
}
}
System.out.print(batsman);
}
However, I get an error. ArrayIndexOutOfException Error.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 1 out of bounds for length 1 at Main.main(Main.java:14)