0

i was coding a program for school in Java with Eclipse and i got a problem with the output, the code is the following:

 public class Main {
    public static boolean[] cercaK(int k , String[] a){
        boolean b[] = new boolean[a.length];
        int conta = 0;
        for(int i = 0; i < a.length; i++){
            b[i] = false;
            int j = 0;
            while(j < a[i].length()-1 && !b[i]){
                    if(a[i].charAt(j)==a[i].charAt(j+1))
                        conta++;
                    else
                        conta = 0;
                    if (conta == k)
                        b[i] = true;
                    j++;
            }
        }
        return b;
    }
    public static void main(String[] args) {
        String[] a = {"accesso","bbtcrryy","ggg"};
        int k = 2;
        if (k<=0)
            System.out.println("Errore!");

        boolean[] b = cercaK(k,a);

        for(int i = 0; i < b.length; i++)
            System.out.print(b);
    }
}

And the output is really strange:

[Z@106d69c[Z@106d69c[Z@106d69c

I can't understand from where these strings come from. Can anyone please help me? Thanks!

EDIT: I found the error,

for(int i = 0; i < b.length; i++)
            System.out.print(b[i]);
Luigi Cerone
  • 362
  • 1
  • 6
  • 18

2 Answers2

0

Change

for(int i = 0; i < b.length; i++)
    System.out.print(b);

to

for(int i = 0; i < b.length; i++)
    System.out.print(b[i]);

So as to print the array's items, not the array itself.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
0

This is because the type of b is boolean[]. When you try to print arrays, you can expect similar output. This is the correct way to print all of the elements:

for(int i = 0; i < b.length; i++)
        System.out.print(b[i]);

Optionally, you can use the for-each loop:

for(boolean var: b) {
  System.out.println(var);
}
Ivaylo Toskov
  • 3,911
  • 3
  • 32
  • 48