我有一个 JavaFX textField,从中我可以得到一个字符串输入。我正在使用 toCharArray() 将字符串作为字符数组传递。但是由于某种无法解释的原因,数组的长度出现在数组之后。任何想法可能导致这种情况?我希望输入是 16 个元素,所以我对其进行了硬编码,但由于某种原因,我得到的结果是 18 个元素,最后两个元素为 1、6。(当我更改输入长度时,最后两个元素跟随)。现在我知道在 Java 中称其为错误是愚蠢的,因此问题出在我的最后,但对于我的生活,我无法弄清楚?
public class something extends Application {
String input;
char[] chars;
public void start(Stage primaryStage) {
TextField field = new TextField("Enter");
input = field.getText();
Button btn = new Button("Check");
btn.setOnAction(e -> validator(input));
}
public void validator(String input) {
chars = new char[input.length()];
System.out.println(input.length()); // this still shows 16
if (chars.length == 16) {
chars = input.toCharArray();
}
for (int i = 0; i < chars.length; i++){
System.out.print(chars[i]);
} //here the problem occurs, when I try to print the array
System.out.println(chars.length); //this also shows 16
if (chars[0] == '4'){
System.out.println("yayy");
check(input);
}
else {
// shows an alert
}
}
}
public void check(String str){
// some other code that works properly
}
public static void main(String[] args) {
Application.launch(args);
}
慕哥6287543
肥皂起泡泡
相关分类