We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys. So far I have a log in that only allows users from the actual code. I would like to change it to be reading and writing to a file? have you guys any advice how id do this?
import static javax.swing.JOptionPane.*;
import javax.swing.JPasswordField;
final StringDict accounts = new StringDict(
new String[] {
"baptiste", "tanguy", "alexis"
}
, new String[] {
"bap", "guy", "ex"
}
);
final JPasswordField pwd = new JPasswordField();
{
println(accounts);
}
void draw() {
if (frameCount == 1) frame.setVisible(false);
String user = askUser();
if (user == null) confirmQuit();
else if (!"".equals(user)) askPass(user);
}
String askUser() {
String id = showInputDialog("Please enter user:");
if (id == null)
showMessageDialog(null, "You've canceled login operation!"
, "Alert", ERROR_MESSAGE);
else if ("".equals(id))
showMessageDialog(null, "Empty user input!"
, "Alert", ERROR_MESSAGE);
else if (!accounts.hasKey(id = id.toLowerCase()))
showMessageDialog(null, "Unknown \"" + id + "\" user!" + (id = "")
, "Alert", ERROR_MESSAGE);
return id;
}
boolean askPass(String id) {
boolean isLogged = false;
pwd.setText("");
int action = showConfirmDialog(null, pwd
, "Now enter password:", OK_CANCEL_OPTION);
if (action != OK_OPTION) {
showMessageDialog(null, "Password input canceled!"
, "Alert", ERROR_MESSAGE);
return false;
}
String phrase = pwd.getText();
//String phrase = new String(pwd.getPassword());
if ("".equals(phrase))
showMessageDialog(null, "Empty password input!"
, "Alert", ERROR_MESSAGE);
else if (accounts.get(id).equals(phrase)) {
showMessageDialog(null, "Welcome \"" + id + "\"!\nYou're logged in!"
, "Info", INFORMATION_MESSAGE);
isLogged = true;
} else
showMessageDialog(null, "Password \"" + phrase + "\" mismatch!"
, "Alert", ERROR_MESSAGE);
return isLogged;
}
void confirmQuit() {
if (showConfirmDialog(null, "Wanna quit then?", "Exit"
, OK_CANCEL_OPTION) == OK_OPTION) exit();
}
Answers
loadStrings() will read a file.
you have nothing that writes to a file.
I believe you've got that from here: https://forum.Processing.org/two/discussion/1724/how-to-create-an-interface-asking-username-and-correct-password
StringDict got methods keyArray() & valueArray() in order to pull out clones of its internal 2 arrays as String[]:
Then you can save those 2 arrays w/ saveStrings():
https://Processing.org/reference/saveStrings_.html
The inverse is also very easy. Load those 2 files via loadStrings():
https://Processing.org/reference/loadStrings_.html
Then instantiate a StringDict passing those 2 loaded String[] as its constructor's arguments:
https://Processing.org/reference/StringDict.html
Yes I did get it from that link :)
so would u change the arrays to be....
Please tell me if im way off :/
When the app starts in setup(), you loadStrings() the 2 files. And use them as arguments for StringDict.
When program exit(), use keyArray() & valueArray(). The saveStrings() those 2 String[] arrays.
Thanks so much for your help but I am still so lost :/ Is there any chance u could write a really short example for me.. It would make it easier for me to understand if I see an example :/ can't find any online :( Thanks :)