I am asking for my JAVA program, I am dealing with the Login Page. In my codes. I have a priming of the Read Methods to get an Array List. Then in Registration Button, once I already inputted a data, btw I used bufferedWriter, so it will append on my existing .txt file.
Then here is my problem , the Array List is already not updated since the data arrayed is only in the priming, means only from the start of the program, what if I already have my new inputs, it will become invalid credentials in my login method.. I tried copy-pasting my priming in the registration button method and private class method but still not working , please help thank you so much.
Here is my codes for my scanner method:
private void initialize() throws FileNotFoundException {
String datas = "";
Scanner notePad = new Scanner(new File("loginData.txt"));
List<String> temps = new ArrayList<String>();
while (notePad.hasNext())
{
datas = notePad.next();
temps.add(datas);
}
notePad.close();
String[] dataFile = temps.toArray(new String[0]);
int mainCount = dataFile.length-1;
and the frame codes follows including my buttons. Skipping to the registration button method, here is the code.
JButton registerButton = new JButton("REGISTER");
registerButton.setFont(new Font("Arial", Font.BOLD, 12));
registerButton.addActionListener(new ActionListener() {
@SuppressWarnings("deprecation")
public void actionPerformed(ActionEvent e)
{
int countRegister = 0;
while (!registerEmail.getText().equalsIgnoreCase(dataFile[countRegister]) && mainCount >= countRegister)
{
countRegister = countRegister + 2;
if (mainCount < countRegister)
{
break;
}
}
if (mainCount <= countRegister) // No Existing Account
{
try {
FileWriter writer = new FileWriter("loginData.txt", true);
BufferedWriter bufferedWriter = new BufferedWriter(writer);
bufferedWriter.newLine();
bufferedWriter.write(registerEmail.getText());
bufferedWriter.newLine();
bufferedWriter.write(registerPassword.getText());
bufferedWriter.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JOptionPane.showMessageDialog(frame, "Registered successfully, you can now login.", "Success", JOptionPane.INFORMATION_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(frame, "Sorry, you have entered an existing account.", "Error", JOptionPane.ERROR_MESSAGE); // Existing Account
}
}
});
registerButton.setBounds(194, 502, 97, 23);
frmImma.getContentPane().add(registerButton);
You can make a method to get the data and compare, before your logic call this method to get the refresh input
private String[] data = new String[0];
private void selectData() throws FileNotFoundException {
Scanner scanner = new Scanner(new File("loginData.txt"));
List<String> temps = new ArrayList<String>();
while (notePad.hasNextLine()) {
temps.add(notePad.nextLine());
}
scanner.close();
dataFile = new String[temps.size()];
dataFile = arrlist.toArray(dataFile);
}
, You can use NIO from JDK8+
private void selectData() throws IOException{
dataFile = Files.lines(Paths.get(new
File("C:\\data.txt").getPath())).toArray(String[]::new);
}
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.