-3

How to create a method that will take imput of a String and an integer n and output the String divided into parts consisting of n characters and separated by a blank space? For example, imput String: "THISISFUN", integer:3, result: "THI SIS FUN".

When you answer, can you please really try to explain what each part of the code does? I really want to understand it.

I tried using StringBuilder and the split() method but the problem is that I don't understand how all of that works. Therefore, I ended up kind of thoughtlessly pasting parts of codes from different online articles which doesn't work the best if you want to actually learn something, especially if you simply cannot find any posts about a specific issue. I could only find things like: "how to divide the String into n parts" and "how to ad a space after a specific char" which are sort of similar issues but not the same.

  • 1
    "I ended up kind of thoughtless pasting parts of codes from different online articles" - how would that be any different from you thoughtlessly pasting someone's solution here? Programming _requires_ thought, you need to understand what the different methods are doing, and develop an approach/solution to your problem. The solution you devise is typically decided before you even program anything. Ask yourself: how would you solve this problem by hand? – Rogue Dec 06 '22 at 18:15
  • Well, here someone could explain this issue to me. I am very new to programming, especially transforming Strings, and the different methods I've seen confuse me because I haven't seen them anywhere properly explained. That's why I specified that when you answer, I'm asking for an additional explanation of how individual elements of the code work so that I am better informed in the future. – Ama123ture Dec 06 '22 at 18:20
  • Have you taken a look at the [javadoc for String](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html)? It's extensively documented, and explains every method you can invoke on a string. – Rogue Dec 06 '22 at 18:23
  • here you have plenty of examples: https://stackoverflow.com/questions/3760152/split-string-to-equal-length-substrings-in-java – wypieprz Dec 06 '22 at 18:30
  • Please provide enough code so others can better understand or reproduce the problem. – Community Dec 14 '22 at 18:19

1 Answers1

0

Here is one way to do it:

public static void splitString(String str, int groupSize){
    char[] arr = str.toCharArray(); // Split the string into character array ①
    
    // Iterate over array and print the characters
    for(int i=0; i<arr.length; i++){
        // If 'i' is a multiple of 'groupSize' ②
        if(i > 0 && i % groupSize == 0){ ③
            System.out.print(" "); 
        }
        
        System.out.print(arr[i]);
    }
}

① Split the string into a character array (so that you can access the characters individually). You can also do it using the charAt() method without splitting the string into an array. Read the Javadoc for more details.

② Check if the loop counter i is a multiple of groupSize

③ Note the use of System.out.print() as we do not want to print a newline. Here you can use a StringBuilder too and print the contents at the end instead of printing the characters inside the loop.

Vini
  • 8,299
  • 11
  • 37
  • 49