I created a GUI Login App where a password when submitted it is inputted into a
ID: 3907188 • 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 have hard coded a password, which it works and the JOption Message success works then right after the JOption Message invalid password message pops up that is at the bottom of the code where the if(valid). I'm not sure how to code the following if to show an invalid password then have the JOption Message appear with invalid message.
private void LogsubmitActionPerformed(java.awt.event.ActionEvent evt) {
try{
String password = jPassword.getText();
String username = jtxtUsername.getText();
File texting = new File("Log.txt");
//open file in append mode
BufferedWriter writer;
writer = new BufferedWriter(new FileWriter(texting,true));
Date currentTime =Calendar.getInstance().getTime();
boolean valid = false;
if(password.equals("letmein")&&(username.equals("deanna")));
{
jtxtUsername.setText(null);
jPassword.setText(null);
}
JOptionPane.showMessageDialog(null, "success");
writer.write(username + currentTime);
writer.write("success");
if(valid);
JOptionPane.showMessageDialog(null, "invalid password");
writer.write("failed");
writer.close();
} catch (IOException ex) {
Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
Explanation / Answer
----------------------------------Original Code---------------------------------
try{
String password = jPassword.getText();
String username = jtxtUsername.getText();
File texting = new File("Log.txt");
//open file in append mode
BufferedWriter writer;
writer = new BufferedWriter(new FileWriter(texting,true));
Date currentTime =Calendar.getInstance().getTime();
boolean valid = false;
if(password.equals("letmein")&&(username.equals("deanna")));
{
jtxtUsername.setText(null);
jPassword.setText(null);
}
JOptionPane.showMessageDialog(null, "success");
writer.write(username + currentTime);
writer.write("success");
if(valid);
JOptionPane.showMessageDialog(null, "invalid password");
writer.write("failed");
writer.close();
} catch (IOException ex) {
Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
----------------------------------Original Code---------------------------------
The code need modification
----------------------------------Modified Code---------------------------------
try{
String password = jPassword.getText();
String username = jtxtUsername.getText();
File texting = new File("Log.txt");
//open file in append mode
BufferedWriter writer;
writer = new BufferedWriter(new FileWriter(texting,true));
Date currentTime =Calendar.getInstance().getTime();
boolean valid = false;
if(password.equals("letmein")&& username.equals("deanna"))
{
jtxtUsername.setText(null);
jPassword.setText(null);
JOptionPane.showMessageDialog(null, "success");
writer.write(username + currentTime);
writer.write("success");
valid = true;
}
else {
JOptionPane.showMessageDialog(null, "invalid password");
writer.write("failed");
}
writer.close();
} catch (IOException ex) {
Logger.getLogger(LoginApp.class.getName()).log(Level.SEVERE, null, ex);
}
}
----------------------------------Modified Code---------------------------------
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.