这里是Java Starter,因此在我的密码生成程序中,
因此,我尝试了一个选项来生成多个密码,并在它们之间留有一个空格。

例:
密码1

密码2

但是,当我将“密码”文本设置为文本字段时,我会不断得到如下内容:ADWADjava.lang.StringQE_Q12

我未在代码中发现任何错误,或导致弹出java.lang.String的错误,如果您知道有什么问题,请回复此问题或发送答案。

我的代码:

    createPass.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                field.setText(null);
                field.setText("");
                String[] pass = new String[5];
                for(int i = 0; i < pass.length ; i++) {
                    pass[i] = allrandom();


                }
                for(int i = 0; i < pass.length ; i++) {
                    field.setText(field.getText() + pass[i]);
                }


            }});



}
    public static String allrandom() {
        String cpass = "";
        password = "";
        String[] pass = new String[15];
        Random r = new Random();
    for(int i = 0 ; i < pass.length ; i++) {
        int ch = 0;
        ch = r.nextInt(3);
        switch(ch) {
        case 0:
            if(passsettings.isIfDots()) {
                String newchar = "";
                newchar = dotpass();
                pass[i] = newchar;
                break;
            }
        case 1:
            if(passsettings.isIfNum()) {
                String newchar2 = "";
                newchar2 = Numpass();
                pass[i] = newchar2;
                break;
            }
        case 2:

            String newchar3 = "";
            newchar3 = charchose();
            pass[i] = newchar3;
            break;
        }

    }
    for(int i = 0 ; i < pass.length ; i++) {
        String newpasschar = "";
        newpasschar = pass[i];
        cpass = pass + newpasschar;
    }
    cpass = cpass + "\n";
    return cpass;


        // random() end
    }


    public static String uppercasepass() {
        String[] abc = {"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        Random r = new Random();
        String passletter = "";
        int rN3 = 0;
        rN3 = r.nextInt(26);
        passletter = abc[rN3];



        return passletter;
    // uppercase() end
}

    public static String lowercasepass() {
        Random r = new Random();
        String passletter = "";
        int rN3 = 0;
        rN3 = r.nextInt(26);
        String[] abc = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
        passletter = abc[rN3];
        return passletter;
    }

    public static String Numpass() {
        Random r = new Random();
        String passletter = "";
        int rN = 0;
        rN = r.nextInt(9);
        String[] nums = {"1","2","3","4","5","6","7","8","9"};
        passletter = nums[rN];
        return passletter;
    }

    public static String dotpass() {
        Random r = new Random();
        String passletter = "";
        int rN5 = 0;
        rN5 = r.nextInt(6);
        String[] dots = {"_","]","$","#","=","@"};
        passletter = dots[rN5];




        return passletter;
    }


    public static String charchose() {
        Random r = new Random();
        String line = "";
        int rN = 0;
        rN = r.nextInt(2);
        String[] random = {"Low","Up"};
        String random2 = random[rN];
        switch(random2) {
        case "Low":
            if(passsettings.isIfLowercase()) {
                line = lowercasepass();
            }else {
                charchose();
            }
        case "Up":
            if(passsettings.isIfUpprcase()) {
                line = uppercasepass();
            }else {
                charchose();
            }
        }










        return line;
    }


谢谢,


Java入门

最佳答案

调试代码时,我遇到了很多问题,但主要是...

public static String allrandom() {
    String cpass = "";
    password = "";
    String[] pass = new String[15];
    //...
    for (int i = 0; i < pass.length; i++) {
        String newpasschar = "";
        newpasschar = pass[i];
        cpass = pass + newpasschar;
    }
    cpass = cpass + "\n";
    return cpass;

    // random() end
}


pass + newpasschar试图连接一个数组和一个String对象。这基本上与pass.toString() + newpasschar.toString()相同

为什么执行此操作,我不知道cpass += newpasschar是否会达到您想要的结果。

我真的不想尝试所有问题并指出所有问题,但可以说...学习如何使用StringBuilder-效率更高

整件事都让我大叫定制类,例如,特别是使用生成器模式。

public class PasswordBuilder {

    private List<String> characters;
    private List<String> numbers;
    private List<String> dots;

    private boolean useDots = true;
    private boolean useNumbers = true;
    private boolean useLowerCase = true;
    private boolean useUpperCase = true;

    private int length;

    public PasswordBuilder(int length) {
        this.length = length;
    }

    public PasswordBuilder useDots(boolean use) {
        useDots = use;
        return this;
    }

    public PasswordBuilder useNumbers(boolean use) {
        useNumbers = use;
        return this;
    }

    public PasswordBuilder userLowerCase(boolean use) {
        useLowerCase = use;
        return this;
    }

    public PasswordBuilder useUpperCase(boolean use) {
        useUpperCase = use;
        return this;
    }

    protected int getLength() {
        return length;
    }

    protected boolean isUsingDots() {
        return useDots;
    }

    protected boolean isUsingNumbers() {
        return useNumbers;
    }

    protected boolean isUsingLowerCase() {
        return useLowerCase;
    }

    protected boolean isUsingUpperCase() {
        return useUpperCase;
    }

    public String build() {
        characters = new ArrayList<>(Arrays.asList("A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"));
        numbers = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"));
        dots = new ArrayList<>(Arrays.asList("_", "]", "$", "#", "=", "@"));

        Random r = new Random();
        StringBuilder sb = new StringBuilder(getLength());
        for (int i = 0; sb.length() < getLength(); i++) {
            switch (r.nextInt(3)) {
                case 0:
                    if (isUsingDots()) {
                        sb.append(dotpass());
                        break;
                    }
                case 1:
                    if (isUsingNumbers()) {
                        sb.append(Numpass());
                        break;
                    }
                case 2:
                    sb.append(charchose());
                    break;
            }
        }
        return sb.toString();
    }

    protected String randomFrom(List<String> values) {
        Collections.shuffle(values);
        return values.get(0);
    }

    protected String uppercasepass() {
        return randomFrom(characters).toLowerCase();
    }

    protected String lowercasepass() {
        return randomFrom(characters).toLowerCase();
    }

    protected String Numpass() {
        return randomFrom(numbers);
    }

    public String dotpass() {
        return randomFrom(dots);
    }

    protected String charchose() {
        Random r = new Random();
        int value = r.nextInt(2);
        String text = "";
        switch (value) {
            case 0:
                text = lowercasepass();
            case 1:
                text = uppercasepass();
        }
        return text;
    }

}


这意味着您可以简单地做类似...

System.out.println(new PasswordBuilder(15).build());
System.out.println(new PasswordBuilder(15).useNumbers(false).build());
System.out.println(new PasswordBuilder(15).userLowerCase(false).build());
System.out.println(new PasswordBuilder(15).userLowerCase(false).useUpperCase(false).build());


并输出类似...

xt7$93l]]#]@$xc
]o#r=#pxhk@lyth
@_s5q6]#8i$h]##
2#ccw_1]m3kaz_1


代码中有很多点可能导致递归(堆栈)溢出,一遍又一遍地调用方法,因此,您确实需要检查可用标志的状态,并确保至少一个是true

您仍然可以使用数组(而不是List),但是基本思想保持不变

09-30 14:19