java - Error with receiving strings to list -
it's program receive n strings list , find duplicate's count each string , print string , number of duplicates in map.
i error , can't find problem!
exception in thread "main" java.lang.indexoutofboundsexception: index: 5, size: 5 @ java.util.arraylist.rangecheck(unknown source) @ java.util.arraylist.get(unknown source) @ q1.main(q1.java:16)
here's code:
import java.util.*; public class q1 { public static void main(string[] args) { scanner sc = new scanner(system.in); map<string,integer> m = new hashmap<string,integer>(); list<string> l = new arraylist<string>(); int n = sc.nextint(); for(int = 1; <= n; i++) { l.add(sc.nextline()); } int count=1; for(int = 0; < l.size(); count = 1) { for(int j = 1; < l.size(); j++){ if(l.get(j)==l.get(i)){ l.remove(j); count++; } } m.put(l.get(i),count); } for(int = 0;i < l.size(); count = 1) { system.out.println(m.get(i)); } } }
you not incrementing i
:
for(int i=0;i<l.size();count=1){
also in code below:
`for(int j = 1; < l.size(); j++){`
value of fixed , j getting incremented infinitely i < l.size()
(always) leads indexoutofboundsexception.
Comments
Post a Comment