将字符串组合成一个复合字符串在Java中

huangapple go评论73阅读模式
英文:

Joining groups of strings to create a compound string in java

问题

我正在尝试创建一个用于激活我的Android应用程序的激活码,它的格式应该是 xxx-xxx-xxx-xxx。我已经编写了一个随机生成包含字母和数字的密钥的方法,如下所示:

// 生成指定长度的随机字符串
public static String generateRandom(int n) {
    // 从这个字符串中随机选择字符
    String AlphaNumericString = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
            + "0123456789" + "abcdefghijklmnopqrstuvwxyz";
    // 创建一个指定大小的AlphaNumericString的StringBuffer
    StringBuilder sb = new StringBuilder(n);
    for (int i = 0; i < n; i++) {
        // 生成一个0到AlphaNumericString长度之间的随机数
        int index = (int)(AlphaNumericString.length() * Math.random());

        // 逐个将字符添加到sb的末尾
        sb.append(AlphaNumericString.charAt(index));
    }
    return sb.toString();
}

这部分代码应该生成指定长度的唯一的字母数字密钥,你可以在参数中提供长度,比如这里我将使用4。

此外,我还有另一个方法,它调用 generateRandom() 四次以获得四组密钥,然后使用连字符 - 将它们连接在一起,如下所示:

// 生成指定长度的密钥
public static String generateKey(int length) {
    StringBuilder key = new StringBuilder();
    for (int i = 0; i < length; i++) {
        if (key.toString().length() == 0) {
            key.append(generateRandom(4));
        } else if (key.toString().length() % 4 == 0) {
            key.append("-");
        } else {
            key.append(generateRandom(4));
        }
    }
    return key.toString();
}

当我运行这个程序时,我期望输出应该是像这样的:xxxx-xxxx-xxxx-xxxx,但实际上我得到的是像这样的:xxxx-xxxxxxxxxxxxxxxx。我可能做错了什么,或者如何才能达到我的目标?

英文:

I am trying to create an activation to be used in activating my android app that looks like this xxx-xxx-xxx-xxx now I have written a random alphanumeric key generator method as show here

    // function to generate a random string of length n
   public static String generateRandom(int n)
    {
    // chose a Character random from this String
    String AlphaNumericString = &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;
            + &quot;0123456789&quot; + &quot;abcdefghijklmnopqrstuvxyz&quot;;
    // create StringBuffer size of AlphaNumericString
    StringBuilder sb = new StringBuilder(n);
    for (int i = 0; i &lt; n; i++) {
        // generate a random number between
        // 0 to AlphaNumericString variable length
        int index = (int)(AlphaNumericString.length() * Math.random());

        // add Character one by one in end of sb
        sb.append(AlphaNumericString .charAt(index));
    }
    return sb.toString();
}

this part is supposed to generate a unique set of alphanumeric keys of length supplied within the parameters in this case i will use 4.

I also have another method that calls the generateRandom() four times to get the four sets of the key and then join them together separating them with a hyphen - as shown bellow

    public static String generateKey(int length){
   StringBuilder key = new StringBuilder();
    for(int i = 0; i&lt;length; i++){
        if(key.toString().length()==0){
            key.append(generateRandom(4));
        }else if(key.toString().length() % 4==0){
           key.append(&quot;-&quot;);
       }else{
           key.append(generateRandom(4));
       }
    }
   return key.toString();
}

Now when I run this program i expect the output to look like this xxxx-xxxx-xxxx-xxxx but what I get looks like this xxxx-xxxxxxxxxxxxxxxx What could I be doing wrong or how can I achieve my target

答案1

得分: 1

public static String generateKey(int length)
{
StringBuilder key = new StringBuilder();

for (int i = 0; i < length - 1; i++)
{
    key.append(generateRandom(4));
    key.append("-");
}
key.append(generateRandom(4));

return key.toString();

}

英文:
public static String generateKey(int length)
{
        StringBuilder key = new StringBuilder();

        for(int i = 0; i&lt;length-1; i++)
        {
                key.append(generateRandom(4));
                key.append(&quot;-&quot;);
        }
        key.append(generateRandom(4));
        
        return key.toString();
    }

for each part, add '-', without checking (if...), and the last one do it after the loop.

答案2

得分: 1

你可以通过在循环内调用 key.append(generateRandom(4)); 并在不是最后一个计数时添加连字符来实现这一点。这样做可以避免在生成的字符串末尾附加一个追加操作。

public static String generateKey(int length) {
    StringBuilder key = new StringBuilder();
    for (int i = 0; i < length; i++) {
        key.append(generateRandom(4));
        if (i < length - 1)
            key.append("-");
    }
    return key.toString();
}
英文:

You can achieve that by calling key.append(generateRandom(4)); within the loop and adding the hyphen on every count that is not the last. Doing that avoids having an append at the end of the generated string.

public static String generateKey(int length){
		   StringBuilder key = new StringBuilder();
		    for(int i = 0; i&lt;length; i++){
		    	key.append(generateRandom(4));
		    	if (i&lt;length-1)
		    		key.append(&quot;-&quot;);
		    }
		   return key.toString();
}

答案3

得分: 0

public static String generateKey(int length) {
StringBuilder key = new StringBuilder();
for (int i = 0; i < length; i++) {
String op = generateRandom(length);
if (i == length - 1)
key.append(op);
else key.append(op).append("-");
}
return key.toString();
}

英文:
public static String generateKey(int length){
        StringBuilder key = new StringBuilder();
        for(int i = 0; i&lt;length; i++){
            String op = generateRandom(length);
            if (i == length-1)
                key.append(op);else key.append(op).append(&quot;-&quot;);
        }
        return key.toString();
    }

huangapple
  • 本文由 发表于 2020年8月13日 20:12:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63394897.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定