我是java新手,我想制作一个输入检查器类,当您输入一个值(无论是int、string还是double)时,它会检查它是否不包含非法字符,如果包含,则返回false。
现在,即使它可能很糟糕并且没有多大意义,我还是设法编写了一个代码来做到这一点,问题是它给了我一个接近索引 17 异常的非法字符范围。
这是代码:
public class InputChecker {
public InputChecker()
{
}
public boolean checkString(String string)
{
Pattern pattern = Pattern.compile("[1234567890,.-\\!'~#\\@*+%{}<>\\[\\]|\"\\_^]");
Matcher matcher = pattern.matcher(string);
return matcher.find();
}
public boolean checkDouble(double theDouble)
{
Pattern pattern = Pattern.compile("[,-!'~#@*+%{}<>\\[\\]|\"\\_^]");
String sDouble = String.valueOf(theDouble);
Matcher matcher = pattern.matcher(sDouble);
return matcher.find();
}
public boolean checkInt(int theInt)
{
Pattern pattern = Pattern.compile("[,-!'~#@*+%{}<>\\[\\]|\"\\_^]");
String sInt = String.valueOf(theInt);
Matcher matcher = pattern.matcher(sInt);
return matcher.find();
}
}
Run Code Online (Sandbox Code Playgroud) 基本上,我需要从fxml文件中检索VBox,并且我正在使用scene.lookup。但是,如果我搜索它,它只会崩溃并显示NullPointerException。
所以我试着打印一下scene.lookup()发现的东西,它只是找到null(duh),但我不明白为什么。
这是我的主班:
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception{
FXMLLoader loader = new FXMLLoader(getClass().getResource("MainWindow.fxml"));
Parent root = loader.load();
Scene scene = new Scene(root, 600, 575);
Controller controller = new Controller();
controller.setScene(scene);
System.out.println(scene.lookup("#vBox"));
stage.setScene(scene);
stage.setTitle("Test");
stage.setResizable(false);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的fxml文件:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<BorderPane fx:id="borderPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="575.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<top>
<ToolBar prefHeight="40.0" prefWidth="200.0" BorderPane.alignment="CENTER">
<items>
<Button fx:id="addBtn" …Run Code Online (Sandbox Code Playgroud)