如何比较输入与文件数据
how to compare input with file data
我正在尝试将用户输入的名称与文件 javaFx 中的现有名称进行比较,如果相同的名称将得到警报,否则将出现另一个 window。但我总是得到新的 window 即使名称存在任何建议?
void submitBu(ActionEvent event) throws FileNotFoundException {
File file=new File("/Users/AL/info.txt");
String name=nameTextField.getText();
Scanner n =new Scanner(file);
//show alert if info already exists in file
while(n.hasNextLine()) {
String s=n.nextLine();
if(s.equalsIgnoreCase(name)) {
displayMessage("Unsuccessfull login. Try again.", AlertType.ERROR);
break;
}
Pane root;
try {
root = FXMLLoader.load(getClass().getResource("/view/CreditCard.fxml"));
Stage stage=new Stage();
stage.setScene(new Scene(root, 600, 400));
stage.setTitle("CrediCard");
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
您想在 info.txt 文件中已经存在相同的警报时显示警报。否则,你想再开一个window。 按照您编写方法的方式,它不会那样做。你的代码逻辑漏洞太多。
这是你用过的while循环。我添加了注释以使每个任务更清晰易懂。
// if there are more names in the file
while(n.hasNextLine())
{
// retrieve a name
String s = n.nextLine();
// check is the inputted name matches with the retrieved name
if(s.equalsIgnoreCase(name))
{
// if it does, show error dialog and exit
displayMessage("Unsuccessfull login. Try again.", AlertType.ERROR);
break;
}
// if it does not, create a new window
Pane root;
try
{
root = FXMLLoader.load(getClass().getResource("/view/CreditCard.fxml"));
Stage stage=new Stage();
stage.setScene(new Scene(root, 600, 400));
stage.setTitle("CrediCard");
stage.show();
}
catch (IOException e) { e.printStackTrace(); }
// exit after one iteration
break;
}
最后一个 break 语句是问题。这里发生的是,您检查文件中的名字是否与输入的名称匹配。如果是,则显示警报,否则显示新 window。 您的方法不会使用输入名称检查文件中的任何其他名称。很容易弄清楚为什么会这样。
如果您想根据输入的名称检查文件中的所有名称,然后,如果 none 的名称与输入的名称匹配,则按照您的说明进行操作。这是可行的方法。
void submitButton(ActionEvent actionEvent)
{
File infoFile = new File("path/to/file");
Scanner scanner = new Scanner(System.in);
String inputName = nameTextField.getText();
while (scanner.hasNextLine())
{
String fileName = scanner.nextLine();
if (inputName.equalsIgnoreCase(fileName))
{
// show alert
return;
}
}
// you will only reach here if none of the file name matches the input name
// show new window
}
我正在尝试将用户输入的名称与文件 javaFx 中的现有名称进行比较,如果相同的名称将得到警报,否则将出现另一个 window。但我总是得到新的 window 即使名称存在任何建议?
void submitBu(ActionEvent event) throws FileNotFoundException {
File file=new File("/Users/AL/info.txt");
String name=nameTextField.getText();
Scanner n =new Scanner(file);
//show alert if info already exists in file
while(n.hasNextLine()) {
String s=n.nextLine();
if(s.equalsIgnoreCase(name)) {
displayMessage("Unsuccessfull login. Try again.", AlertType.ERROR);
break;
}
Pane root;
try {
root = FXMLLoader.load(getClass().getResource("/view/CreditCard.fxml"));
Stage stage=new Stage();
stage.setScene(new Scene(root, 600, 400));
stage.setTitle("CrediCard");
stage.show();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
您想在 info.txt 文件中已经存在相同的警报时显示警报。否则,你想再开一个window。 按照您编写方法的方式,它不会那样做。你的代码逻辑漏洞太多。
这是你用过的while循环。我添加了注释以使每个任务更清晰易懂。
// if there are more names in the file
while(n.hasNextLine())
{
// retrieve a name
String s = n.nextLine();
// check is the inputted name matches with the retrieved name
if(s.equalsIgnoreCase(name))
{
// if it does, show error dialog and exit
displayMessage("Unsuccessfull login. Try again.", AlertType.ERROR);
break;
}
// if it does not, create a new window
Pane root;
try
{
root = FXMLLoader.load(getClass().getResource("/view/CreditCard.fxml"));
Stage stage=new Stage();
stage.setScene(new Scene(root, 600, 400));
stage.setTitle("CrediCard");
stage.show();
}
catch (IOException e) { e.printStackTrace(); }
// exit after one iteration
break;
}
最后一个 break 语句是问题。这里发生的是,您检查文件中的名字是否与输入的名称匹配。如果是,则显示警报,否则显示新 window。 您的方法不会使用输入名称检查文件中的任何其他名称。很容易弄清楚为什么会这样。
如果您想根据输入的名称检查文件中的所有名称,然后,如果 none 的名称与输入的名称匹配,则按照您的说明进行操作。这是可行的方法。
void submitButton(ActionEvent actionEvent)
{
File infoFile = new File("path/to/file");
Scanner scanner = new Scanner(System.in);
String inputName = nameTextField.getText();
while (scanner.hasNextLine())
{
String fileName = scanner.nextLine();
if (inputName.equalsIgnoreCase(fileName))
{
// show alert
return;
}
}
// you will only reach here if none of the file name matches the input name
// show new window
}