0

How can I make the password saved from a txt file not in a console

Here is my code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class password{ private static String password = "pass" //Thanks! public static void main(String [] args){ JFrame a = new JFrame("Password"); a.setSize(400,100); a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); a.setVisible(true); JLabel label = new JLabel("Enter password"); JPanel panel = new JPanel(); a.add(panel); JPasswordField pass = new JPasswordField(10); pass.setEchoChar('*'); pass.addActionListener(new Action()); panel.add(label, BorderLayout.WEST); panel.add(pass, BorderLayout.EAST); } static class Action implements ActionListener{ public void actionPerformed(ActionEvent e){ JPasswordField input = (JPasswordField) e.getSource(); char[]passy = input.getPassword(); String p = new String (passy); if(p.equals(password)){ JOptionPane.showMessageDialog(null,"Correct"); }else{ JOptionPane.showMessageDialog(null,"Incorrect"); } } } }

21st Nov 2018, 9:13 AM
Reginald Tobias
Reginald Tobias - avatar
1 Answer
0
Use the File class then read from it with scanner: import java.io.File; import java.io.IOException; import java.util.Scanner; File f = new File(C:\directory.txt); if(!f.exists()){ try{ f.createNewFile(); } catch(IOexception e){ e.printStackTrace(); } } Scanner scan = new Scanner(file); String pass = scan.next(); //password from file directory
30th Nov 2018, 12:45 AM
Devon
Devon - avatar