Hi. I think I have done my code correctly. What I have found out is that error: java.lang.ArrayIndexOutOfBoundsException: 20 means that I have tried to reach 20th number in an array and this number is outside an array. Well array has 20 numbers inside so it should not happen. Can you please help?
package pl.codegym.task.task07.task0701;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/*
Maksimum w tablicy
*/
public class Solution {
public static void main(String[] args) throws Exception {
int[] tablica = inicjalizujTablice();
int max = max(tablica);
System.out.println(max);
}
public static int[] inicjalizujTablice() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int[] tablica = new int[20];
for (int i = 0; i<tablica.length; i++) {
tablica[i] = Integer.parseInt(reader.readLine());
}
return tablica;
}
public static int max(int[] tablica) {
int max = tablica[20];
for (int i = 0; i<tablica.length; i++) {
if (tablica[i]>max) {
max = tablica[i];
}
}
return max;
}
}