Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[Java] Program nie generuje kluczy >100

Ostatnio zmodyfikowano 2016-03-06 11:06
Autor Wiadomość
C-Objective
Temat założony przez niniejszego użytkownika
[Java] Program nie generuje kluczy >100
» 2016-03-06 11:06:29
Witam. Ten kod nie generuje klucza >100 zn. nawet gdy jest o to proszony. Czy tutaj znajduje się błąd?

package dialogs;

import java.io.IOException;
import java.util.Random;

public class AdvKey {
byte[] concatenateByteArrays(byte[] a, byte[] b) {
    byte[] result = new byte[a.length + b.length];
    System.arraycopy(a, 0, result, 0, a.length);
    System.arraycopy(b, 0, result, a.length, b.length);
    return result;
}
private byte[] arr=null;
private byte[][] characters={"!@#$%^&*()_+=".getBytes(),
  "1234567890".getBytes(),
  "qwertyuiopasdfghjklzxcvbnm".getBytes(),
  "QWERTYUIOPASDFGHJKLZXCVBNM".getBytes()};
public AdvKey(int size, boolean specials, boolean numbers, boolean charactersb, boolean bigCharacters) throws IOException {
byte[] characters1="1234567890".getBytes();
if(specials){
characters1=concatenateByteArrays(characters1, characters[0]);
}
if(numbers){
characters1=concatenateByteArrays(characters1, characters[1]);
}
if(charactersb){
characters1=concatenateByteArrays(characters1, characters[2]);
}
if(bigCharacters){
characters1=concatenateByteArrays(characters1, characters[3]);
}
arr=new byte[size];
Random r = new Random();
r.setSeed(System.currentTimeMillis());
for(int i = 0; i < arr.length; i++){
arr[i]=characters1[r.nextInt((characters1.length - 0) + 1) + 0];
}
}
@Override
public String toString(){
return new String(arr);
}
}
Wywołuję to tak:

String output = new AdvKey(200, true, true, true, true).toString();

I jakoś nie widzę błędu...

Przy okazji generuje się coś takiego:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 85 (W środku for'a)
at dialogs.AdvKey.<init>(AdvKey.java:37)
at dialogs.GeneratePass$2.actionPerformed(GeneratePass.java:239)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

PS. Już mam!
Wystarczyło zamienić to co jest w forze na
arr[i]=characters1[r.nextInt(characters1.length)]
;
P-145608
« 1 »
  Strona 1 z 1