0

I am trying to break down a long given string into a smaller string of given length x, and it returns an array of these small strings. But I couldn't print out, it gives me error [Ljava.lang.String;@6d06d69c Please take a look at my code and help me out if I am doing wrong. Thanks so much!

public static String[] splitByNumber(String str, int num) {

    int inLength = str.length();
    int arrayLength = inLength / num;
    int left=inLength%num;
    if(left>0){++arrayLength;}
    String ar[] = new String[arrayLength];
        String tempText=str;
        for (int x = 0; x < arrayLength; ++x) {

            if(tempText.length()>num){
            ar[x]=tempText.substring(0, num);
            tempText=tempText.substring(num);
            }else{
                ar[x]=tempText;
            }

        }


    return ar;
}

public static void main(String[] args) {
    String[] str = splitByNumber("This is a test", 4);
    System.out.println(str);
}
Michael Ng
  • 33
  • 1
  • 6

1 Answers1

0

You're printing the array itself. You want to print the elements.

String[] str = splitByNumber("This is a test", 4);
for (String s : str) {
    System.out.println(s);
}
EdgeCase
  • 4,719
  • 16
  • 45
  • 73