java的;将文本设置为字符串

时间:2017-09-14 08:20:58

标签: java jframe

这是Java Starter,所以在我的密码生成计划中, 所以,我尝试选择生成多个密码,并在它们之间留一个空格。

实施例: PASSWORD1

密码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 Starter

1 个答案:

答案 0 :(得分:1)

在调试代码时,我遇到了(很多事情,但主要是)......

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 s),但基本思想保持不变