Question

I made a code which for change password in a local program i made. It is working almost very well, but i ran into a little issue. If i change the new password to "" (nothing), the file which are reading the password, doesnt works if i leave the field empty. It works fine if i type in a normal password like "hey". The password is stored in a txt file, so the problem is, that an empty JTextField doesnt equal an empty log file.. why? I need a fix of this. I would like it to work with the password (nothing), but some alternative fixes would be cool too.

This code checks password:

check is the string from readfile, and password is the string from my textfield.

if(event.getSource()==tf&&password.equals(check)){
  try{
    Runtime.getRuntime().exec("C:\\Windows\\System32\\notepad C:\\Applications\\Infostore.txt");
  }catch(IOException ex){
    ex.printStackTrace();
  }
  System.exit(1);
}else{
  JOptionPane.showMessageDialog(null,"Wrong password");
}

The password creater is an ordinary filewriter, which gets content from textfield, and write it in a txt file.

Was it helpful?

Solution

Test the value of your JTextField before saving the password.

if (textField.getText()==null) passwd = "";

OTHER TIPS

When the scanner reads your text file, if it reads an empty string at the end of the file it will assume the string is null. This is why it's probably always better to, when finished writing to a file, add a new line at the end of the file using BufferedWriter.newLine().

Otherwise, if you wish to stick with not adding a new line, or it does not work, try adding this to your password check:

if(check == null)

The reason why checking if the string is empty is, like I said before, possibly because the scanner thought it was the end of the file and returned the check string to be null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top