0

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)

Gugan
  • 11
  • 1
  • 3
    FIrst, you should print each line to verify that it has commas. Second, even if it did split on commas, you are not going to parse any of those substrings as an int. (except maybe the last one). – WJS Nov 05 '21 at 17:00
  • 1
    You don't appear to be handling the end of line token correctly – Hovercraft Full Of Eels Nov 05 '21 at 17:02

0 Answers0