我写了一个程序,我想要一些以大写字母+数字开头的输出。然后从允许的字符中随机生成一些。
但我发现输出包含如下空格:k r w ea
. 而我期望:W3krwea
。
我不知道为什么这些空格会出现在我的输出中。
import java.util.Random;
import sun.security.util.Password;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author taskin
*/
public class RPG
{
public static String generate(String allowedCharacters,
boolean mustHaveUppercase, boolean mustHaveDigit,
int minLength, int maxLength)
{
Random rand=new Random();
String rp = null; // Random Password
int Length=allowedCharacters.length();
char[] password=new char[25];
int [] asc=new int[18];
char[] UpperCase=new char[25];
char[] Digit=new char[25];
char[] Mixed=new char[25];
int point1=0,point2=0,point3=0;
for (int i=0;i<Length;i++)
{
asc[i]=(int)allowedCharacters.charAt(i);
}
for (int k=0;k<Length;k++)
{
if (asc[k]>=65 && asc[k]<=90)
{
UpperCase[point1]=(char)asc[k];
point1++;
continue;
}
else if (asc[k]>=48 && asc[k]<=57)
{
Digit[point2]=(char)asc[k];
point2++;
continue;
}
else
{
Mixed[point3]=(char)asc[k];
point3++;
}
}
StringBuilder strbld=new StringBuilder();
int length=maxLength-minLength+1+minLength;
strbld.append(UpperCase[rand.nextInt(UpperCase.length)]);//get a uppercase char
strbld.append(Digit[rand.nextInt(Digit.length)]);//get a digit
for(int i=0;i<length-2;i++){
strbld.append(Mixed[rand.nextInt(Mixed.length)]);
}
rp=strbld.toString();
// Your code to set rp appropriately goes here
return rp;
}
public static void main(String[] args)
{
String allowedCharacters = "weakPasSWorD1234$*";
boolean mustHaveUppercase = true;
boolean mustHaveDigit = true;
int minLength = 3;
int maxLength = 20;
String randomPassword = generate(allowedCharacters,
mustHaveUppercase, mustHaveDigit, minLength,
maxLength);
System.out.println("Random password: " + randomPassword);
}
}