Skip to content
Advertisement

how to compare input with file data

I’m trying to compare user input name to the exists names in the file javaFx if it the same name will get alert if not another window will appear. but I always get new window even if the name exists any suggestion?

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;
        
        
        
        }
    }
    

Advertisement

Answer

You want to display an alert if the same is already present in the info.txt file. Otherwise, you want to open another window. The way your method has been written, it does not do that. There are too many logical flaws in your code.

Here is the while loop that you have used. I have added comments to make each task more clear to understand.

// 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;            
}

The last break statement is the problem. What is happening here is, you check if the first name in the file matches the input name. If it does, show alert, else show new window. Your method will not check any other name in the file with the input name. It’s easy to figure out why that happens.

If you want to check all the names in the file against the input name and then, if none of the name matches the input name then do what you stated. Here is the way that will work.

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
} 
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement