2014-10-30 171 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();