0

Java code

import java.util.*; import java.util.Collections; import java.util.Set; import java.util.Scanner; import java.util.List; class students { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter number of students"); int n= sc.nextInt(); String[] str = new String[n]; System.out.println("Please enter array elements"); for (int i = 0; i <=n; i++) { str[i] = sc.nextLine(); } List<String> list = Arrays.asList(str); Set<String> s = new HashSet<String>(list); for (String freq : s ) { System.out.println(freq + "-->" + Collections.frequency(list, freq)); } } } I can u say wts wrng in this code????

30th May 2020, 11:01 AM
Haritha Vuppula
Haritha Vuppula - avatar
4 RĂ©ponses
+ 2
You have space for n elements but you are trying to store n+1 elements.
30th May 2020, 11:09 AM
Avinesh
Avinesh - avatar
+ 2
1st for loop. It should be i < n.
30th May 2020, 11:11 AM
Avinesh
Avinesh - avatar
0
which line??? i didnt understood
30th May 2020, 11:10 AM
Haritha Vuppula
Haritha Vuppula - avatar
0
I don't find...but don't forget to close the Scanner with sc.close()
1st Jun 2020, 6:45 AM
Vincent Lefebvre
Vincent Lefebvre - avatar