I created a GUI Login App where a password when submitted it is inputted into a
ID: 3906902 • Letter: I
Question
I created a GUI Login App where a password when submitted it is inputted into a log.txt file. The log should include the username, the date, the time and if the login was successful or not. Can you help with the Java code? I'm using javax.swing.JOptionPane library Below is what I started
private void LogsubmitActionPerformed(java.awt.event.ActionEvent evt) {
String password = jPassword.getText();
String username = jtxtUsername.getText();
if (password.contains("letmein") && (username.contains("anna")))
{
jtxtUsername.setText(null);
jPassword.setText(null);
//Creating log file
Writer writer = null;
File check = new File("Log.txt");
if(check.exists()){
//Checks if the file exists.
}else{
try{
File texting = new File("Log.txt");
writer = new BufferedWriter(new FileWriter(texting));
writer.write("message");
String jtxtUsername = " ";
String jPassword = " ";
String user = logUser.getText();
String pass = logPassword.getText();
if (check.exists()) { //Process the password.
byte[]input = jPassword.getBytes();
Explanation / Answer
You can use the following code to do log the user name , time and status. You will need to refresh your project folder (The topmost folder of your project) to find the log.txt file created.
try{
String username = jtxtUsername.getText();
String password = jPassword.getText();
File texting = new File("Log.txt");
BufferedWriter writer = new BufferedWriter(new FileWriter(texting, true)); //open file in append mode
Date currentTime = Calendar.getInstance().getTime();
boolean valid = false;
if (password.equals("letmein") && (username.equals("anna")))
{
jtxtUsername.setText(null);
jPassword.setText(null);
valid = true;
}
else
JOptionPane.showMessageDialog(null,"Invalid credentials");
writer.write(username + " " + currentTime + " " );
if(valid)
writer.write(" success");
else
writer.write(" failed");
writer.close();
}catch(IOException e){
System.out.println(e.getMessage());
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.