Click here to Skip to main content
14,036,604 members
Rate this:
Please Sign up or sign in to vote.
I am writing an encryption program for a course I am taking to learn Java. The method is supposed to split text into chunks, chunk size specified by user. ("3" = "XXX XXX XXX") I am then supposed to pad the end of the String with "x"s when the String doesn't split evenly. I have not written the part to pad the "x"s at the end yet, because my method is leaving the "oddballs" at the beginning. ("3" = "X XXX XXX XXX" or "XX XXX XXX" INSTEAD OF "XXX XXX XX" or "XXX XXX X") I have written my method below. Any advice would be greatly appreciated!

What I have tried:

public static String groupify(String str, int n){

        if(str.length() == 0)
            return str;
        char c = str.charAt(0);
        String s = " ";
        if(str.length() % n == 1)
            return c + s + groupify(str.substring(1), n);
        return c + groupify(str.substring(1), n);
Updated 29-Sep-18 4:27am
Gerry Schmitz 27-Sep-18 17:23pm
We don't do "code reviews".

You "ask a question"; we try to answer. That's it.
Ryan Huffman 27-Sep-18 18:16pm
I only included code, because I figured it was needed. How could someone help me find my error without the code? I'm honestly just trying to understand. I have Googled this at least 10 times (different wording every time) and read everything it brought up that seemed even somewhat related. I have posted on programming Facebook sites. I have been trying to figure it out on my own by changing the code. I'm at a loss at this point.
Richard MacCutchan 28-Sep-18 3:19am
Starting at the beginning of the string.
- if the string length is less than or equal to n, return the string.
- take the first substring of length n and add it to your chunk set
- repeat until no more data.
Member 14000163 28-Sep-18 16:01pm
Thank you very much, Richard MacCutchan!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Cookies | Terms of Service
Web04 | 2.8.190424.1 | Last Updated 29 Sep 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100