-1

I want my String in Java to end up being a multiple of 5 by adding X's to it:

Example 1:

"ABCDEFGHIJK" // length 11

I want to add 4 "X" to the end:

"ABCDEFGHIJKXXXX" // length 15

Example 2:

"ABCDEFGHI" // length 9

I want to add 1 "X" to the end

"ABCDEFGHIX" // length 10

How can I do this? Thanks for any help!

EDIT: Yes I did code some I just forgot to put it in my question

int myInitialAmountOfX = myString.length() % 5;
System.out.println(myInitialAmountOfX);
int myTotalAmountOfX = 5 - myInitialAmountOfX;

Was just confused on how to append.

Coder117
  • 801
  • 2
  • 9
  • 22
  • 1
    What exactly is your problem: calculating the number of `X`s or appending them? – PM 77-1 May 18 '17 at 19:55
  • Please post the code you have written until now. SO is not for writing complete code for you! – PoisonedYouth May 18 '17 at 19:55
  • This smells like homework, and as others have pointed out you need to at least make an attempt before someone will be very willing to help. To point you in the right direction, consider that `5 - (length(input) % 5)` will tell you how many to append, after that you just need to figure out the logic for padding, which could be done in a few ways. – zzevannn May 18 '17 at 19:57
  • Welcome to Stack Overflow! Please [take the tour](http://stackoverflow.com/tour) to see how the site works and what questions are on topic here, and edit your question accordingly. See also: [Why is "Can someone help me?" not an actual question?](http://meta.stackoverflow.com/q/284236) – Joe C May 18 '17 at 19:57
  • @PM77-1 appending them, I know I can use modulus – Coder117 May 18 '17 at 19:57
  • You can append one `X` at a time in a loop. If you already know `StringBuilder` class you might want to use it. – PM 77-1 May 18 '17 at 20:00
  • read this http://stackoverflow.com/questions/1235179/simple-way-to-repeat-a-string-in-java – Youcef LAIDANI May 18 '17 at 20:00
  • @PM77-1 That's right I completely forgot, no this isn't homework everyone and I forgot to attach some code. – Coder117 May 18 '17 at 20:03
  • Indeed, as noted by YCF_L, this Question seems to be a duplicate of: [Simple way to repeat a String in java](http://stackoverflow.com/q/1235179/642706). Some Answers there are clever, such as using [`Collections.nCopies`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#nCopies-int-T-). – Basil Bourque May 18 '17 at 21:15

3 Answers3

2

To find out the number of characters, that need to be appended, you can use

(5-(s.length()%5)) I hope this is self explanatory.

And for appending a simple for loop, would do

for(int i=1;i<=j;i++){
    s+="X";
}

However, for larger Strings, you might want to prefer StringBuilder

dumbPotato21
  • 5,669
  • 5
  • 21
  • 34
  • 2
    You can also use `new String(new char[j]).replace("\0", "X");` for a nice one liner. Found [here](http://stackoverflow.com/a/4903603/5817001). – fqhv May 18 '17 at 20:07
1

Something like:

String input = "ABCDEFGHIJK";

while ( input.length() % 5 != 0 )
    input += "X";
SomeDude
  • 13,876
  • 5
  • 21
  • 44
-2
package test;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

public class PadTest {

    public static void main(String[] args) throws IOException {

        String str = "Pad Me";
        System.out.printf("%-15s[%s]\n", "Original:", str);

        System.out.println();

        System.out.printf("%-15s[%s]\n", "Left Padded:", StringUtils.leftPad(str, 10));
        System.out.printf("%-15s[%s]\n", "Right Padded:", StringUtils.rightPad(str, 10));
        System.out.printf("%-15s[%s]\n", "Centered:", StringUtils.center(str, 10));

        System.out.println();

        System.out.printf("%-15s[%s]\n", "Left Padded:", StringUtils.leftPad(str, 10, "*"));
        System.out.printf("%-15s[%s]\n", "Right Padded:", StringUtils.rightPad(str, 10, "*"));
        System.out.printf("%-15s[%s]\n", "Centered:", StringUtils.center(str, 10, "*"));

    }

}

The output from executing PadTest is shown below. Results

Original:      [Pad Me]

Left Padded:   [    Pad Me]
Right Padded:  [Pad Me    ]
Centered:      [  Pad Me  ]

Left Padded:   [****Pad Me]
Right Padded:  [Pad Me****]
Centered:      [**Pad Me**]

Font: http://www.avajava.com/tutorials/lessons/how-do-i-pad-a-string-with-spaces-or-other-characters.html

Marcos Paulo
  • 120
  • 6
  • Stack Overflow is a question-and-answer site, not a homework writing service. Please do not give users reason to believe otherwise. Thank you. – Joe C May 18 '17 at 19:56
  • 1
    how does this answer the question above? – ΦXocę 웃 Пepeúpa ツ May 18 '17 at 19:57
  • @JoeC Sharing information cant be unhelpful. I would have done the same way – Luatic May 18 '17 at 19:57
  • 1
    @user7185318 I would argue that an answer like this is unhelpful because the asker can simply copy-and-paste it into whatever they're doing without any need to understand what is going on. If this is a homework assignment (as I suspect it is), it deprives the asker of the opportunity to improve problem-solving skills that the said assignment is meant to provide. – Joe C May 18 '17 at 20:01
  • @JoeC exactly! Have to educate users of SO – PoisonedYouth May 18 '17 at 20:02
  • @JoeC Hi Joe! I just forgot about the StringBuilder class! I added my code where I knew about the modulus, next time I will be sure to include more code so that you don't suspect I know nothing. – Coder117 May 18 '17 at 20:08