2014-10-30 170 views
2
import java.util.Random; 

public class PasswordRandomizer { 
    // Define the variables 
    private Random random = new Random(); 
    private int passwordLength; 
    private String password = ""; 

    public PasswordRandomizer(int length) { 
     // Initialize the variable 
     this.passwordLength = length; 
    } 

    public String createPassword() { 
     // write code that returns a randomized password 
     for(int i = 0; i < this.passwordLength; i++){ 
      int j = random.nextInt(); 
      char symbol = "abcdefghijklmnopqrstuvwxyz".charAt(j); 
      this.password = this.password + symbol; 
     } 
     return this.password; 
    } 
} 

我如何添加字符到一个字符串,我试过,但我得到这个错误:字符添加到字符串

"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -414383904".

+0

如果i = 5“abcde”,我不会打印出来,我发现一个错误,我没有设置随机数的限制,这给出了一些错误,但它仍然无法正常工作。 – UkoM 2014-10-30 17:30:29

回答

7

这是因为random.nextInt()回报-2,147,483,648和2,147,483,647之间的值。

你想要的是random.nextInt("abcdefghijklmnopqrstuvwxyz".length())

我还要分配给"abcdefghijklmnopqrstuvwxyz"恒定。

private final static String ALPHABET = "abcdefghijklmnopqrstuvwxyz"; 

Char randomChar = ALPHABET.charAt(random.nextInt(ALPHABET.length())); 
+4

它实际上是范围[-2,147,483,648,2,147,483,647] – clcto 2014-10-30 17:28:53

+0

谢谢,我编辑了我的答案。 – 2014-10-30 17:32:18

1

你的问题不是串联而是随机生成

int j = random.nextInt(); 
char symbol = "abcdefghijklmnopqrstuvwxyz".charAt(j); 

您需要使用nextInt与区间震荡

1

你正在使用Random.nextInt没有任何约束。此方法返回2^32种可能性的任何整数。使用有界Random.nextInt("abcdefghijklmnopqrstuvwxyz".length())

1

试试这个:

int j = random.nextInt(26); 

可能的值将在0-25之间包含在内,这将符合您的字母索引。

1

这就是问题所在:

int j = random.nextInt(); 
char symbol = "abcdefghijklmnopqrstuvwxyz".charAt(j); 

charAt方法要求它的参数是字符串的范围内 - 你仅仅使用从Random.nextInt()一个随机整数这可能会对任何int值:

Returns the next pseudorandom, uniformly distributed int value from this random number generator's sequence. The general contract of nextInt is that one int value is pseudorandomly generated and returned. All 232 possible int values are produced with (approximately) equal probability.

你应该使用类似:

private static String final ALPHABET = "abcdefghijklmnopqrstuvwxyz"; 
... 
int j = random.nextInt(ALPHABET.length()); 
char symbol = ALPHABET.charAt(j); 

这样你就知道j将在字符串的范围之内(即, 0 <= j < ALPHABET.length())。

还有其他的事情,我会改变你的代码,但:

  • 没有必要使用此字符串连接
  • 没有必要用一个实例变量Random(目前为您生成每个密码会比前一个)
  • 我会用最后的领域
  • 我会使用SecureRandom,而不是Random的密码生成
  • 长0
  • 我会做出最终的类
  • 我会允许字符的字母在

所以传递:

public final class PasswordRandomizer { 
    private static final String DEFAULT_ALPHABET = "abcdefghijklmnopqrstuvwxyz"; 
    private final Random random = new SecureRandom(); 
    private final String alphabet; 
    private final int passwordLength; 

    public PasswordRandomizer(int length) { 
     this(length, DEFAULT_ALPHABET); 
    } 

    public PasswordRandomizer(int length, String alphabet) { 
     // TODO: Arguvment validation 
     this.passwordLength = length; 
     this.alphabet = alphabet; 
    } 

    public String createPassword() { 
     char[] chars = new char[passwordLength]; 

     for (int i = 0; i < this.passwordLength; i++){ 
      chars[i] = alphabet.charAt(random.nextInt(alphabet.length()); 
     } 
     return new String(chars); 
    } 
} 
0

代码random.nextInt()创造出比长度更值“abcdefghijklmnopqrstuvwxyz”中的字符。

使用0到25之间的值。使用random.nextInt(25);而不是random.nextInt();