0
String[] kelas = {" rizki" ," rifka"," putri"," sahal"};
    
    System.out.println(Arrays.toString(kelas));
    
    System.out.println( kelas[0]);
    for ( int i=0; 0 <= kelas.length; i++)
     {
        System.out.println("teman ke-"+1+ " adalah" + kelas[i]);
    }

hello i am try to make an array that will end like this :

[rizki,  rifka, putri, sahal]
rizki
teman ke-1 adalahrizki
teman ke-2 adalah rifka
teman ke-3 adalahputri
teman ke-4 adalahsahal

but in the end it said this

[rizki,  rifka, putri, sahal]
rizki
teman ke-1 adalahrizki
teman ke-1 adalah rifka
teman ke-1 adalahputri
teman ke-1 adalahsahal
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
   at javaapplication7.JavaApplication7.main(JavaApplication7.java:34)
C:\Users\Rizki Sultan Afian\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53:Java  
returned: 1
BUILD FAILED (total time: 8 seconds)

and this is the image:the array

i still dont understand java but i need to finish it for homework.

1 Answers1

2

This line is wrong:

for ( int i=0; 0 <= kelas.length; i++)

Should be:

for ( int i=0; i < kelas.length; i++)
Jesper
  • 202,709
  • 46
  • 318
  • 350