I'm trying to make a JFrame which checks to see if text placed in a JTextField are the same as the information in a .txt file. Then if that's correct, it checks to see if the information in the JPasswordField match the information in the .txt file.
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == button)
{
if(!username.getText().equals("") && passIsGood())
{
try
{
Scanner reader = new Scanner(new File("users.txt"));
reader.useDelimiter("\t|\n");
reader.nextLine();
while(reader.hasNextLine())
{
if(reader.next().equals(username.getText()))
{
//Username is correct, now checks for password:
System.out.println("Username passed");
if(reader.next().equals(passToString()))
{
//Username & Password are correct
System.out.println("Password passed");
break;
}
} else if(!reader.hasNextLine())
{
wrongInfo();
} else
{
reader.nextLine();
}
}
reader.close();
frame.dispose();
} catch(Exception ex)
{
JOptionPane.showMessageDialog(null, ex);
}
} else
{
JOptionPane.showMessageDialog(null, "Please enter a valid Username and Password");
}
}
}
private static String passToString()
{
String passString = new String(password.getPassword());
return passString;
}
I get as far as the username passing, but I feel like the .equals(passToString()) may be causing the problem. The text file looks like this: Username Password test thetest test2 thetest3
Note that the spaces in between each field are tabs, hence my delimiter using ("\\t|\\n");
The Strings contained in your textfields may contain some unwanted characters. Use a character domain (all ascii values that are a symbol) to get rid of those. Try this:
import java.io.File;
import java.util.Scanner;
public class readUsers {
public static void main(String[] args) {
try {
Scanner reader = new Scanner(new File("users.txt"));
reader.useDelimiter("\t|\n");
String username = "username";
String password = "password";
for (int i = 0; i < username.length(); i++) {
//replace possible unwanted characters
if (username.charAt(i) < 33 || username.charAt(i) > 126)
username = username.replace(Character.toString(username.charAt(i)), "");
}
for (int j = 0; j < password.length(); j++) {
//replace possible unwanted characters
if (password.charAt(j) < 33 || password.charAt(j) > 126)
password = password.replace(Character.toString(password.charAt(j)), "");
}
while (reader.hasNextLine()) {
String user = reader.next();
String pass = reader.next();
//if username equals the user in the textfile, check if password equals the pass in the file
if (user.equals(username)) {
System.out.println("username passed!");
if (pass.equals(password))
System.out.println("password passed!");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
My textfile looks like this:
username password
username2 password
PS This is my first StackOverFlow answer ever, correct me if I do anything wrong :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.