gpt4 book ai didi

java ;将文本设置为字符串时不断获取 java.lang.String

转载 作者:行者123 更新时间:2023-12-02 12:15:29 25 4
gpt4 key购买 nike

这里是 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

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

关于 java ;将文本设置为字符串时不断获取 java.lang.String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46213991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com