public class Solution {
private static List<String> strings;
public static void main(String[] args) throws Exception {
//write your code here
strings = new ArrayList<String>();
for (int i = 0; i < 5; i++){
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
strings.add(reader.readLine());
}
int longest = strings.get(0).length();
for (int i = 1; i <= strings.size(); i++){
for (int j = 1; j <= strings.size(); j++){
if (longest < strings.get(j).length())
longest = strings.get(j).length();
}
}
for (int i = 0; i < strings.size(); i++){
if (longest == strings.get(0).length()){
System.out.println(strings.get(i));
}
}
}
}
Andrei
Level 41
I get the " Exception in thread "main" java.lang.IndexOutOfBoundsException: Index 5 out of bounds for length 5 " error - pls help?
Resolved
Comments (4)
- Popular
- New
- Old
You must be signed in to leave a comment
Misiu
23 October 2020, 15:38solution
If list size is 5 then indexes are 0, 1, 2, 3, 4.
j may not be 5.
Line 23 needs correcting/
Why two loops, outer and inner?
+2
Andrei
26 October 2020, 08:41
Hi Misiu, thank you kindly for your answer!
I have gone over the program I wrote, I believe it was my fourth attempt or so, so at that point I was more confused than anything else, haha!
I rewrote it having in mind your suggestions and it worked! Below you can see it. If you have any suggestions on how I could improve my code considering my level in Codegym, these would be much appreciated!
Thanks!
Andrei.
0
Misiu
26 October 2020, 12:07useful
Move up line 9, before FOR. One reader is enough.
Close stream reader after that FOR.
+1
Andrei
26 October 2020, 12:29
OK, thank you!
0