Here is my code:
public class Main extends JFrame{
static int NoOfUsers;
static String[][] Accounts = new String[NoOfUsers][2];
public static void main(String[] args){
Login();
}
private static void Login() {
final String FileName = "F:/TextFiles/loginaccs.txt";
try {
BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(FileName)));
int NoOfUsersL = Integer.parseInt(file.readLine());
String[][] Accounts = new String[NoOfUsersL][2];
NoOfUsers = NoOfUsersL;
for (int i=0; i<NoOfUsersL; i++) {
Accounts[i][0] = file.readLine();
Accounts[i][1] = file.readLine();
}
for (int i=0; i<NoOfUsersL; i++) {
System.out.println(Accounts[i][0]);
System.out.println(Accounts[i][1]);
}
file.close();
} catch (IOException e) {
System.out.println("ERROR: unable to read file.");
e.printStackTrace();
}
String username = null;
String password = null;
JTextField usernamejtf = new JTextField(username);
JPasswordField passwordjtf = new JPasswordField(password);
String[] buttons = {"Login", "Create new account"};
Object[] InputDialog = {
"Username: ", usernamejtf, "Password: ", passwordjtf
};
do {
int option = JOptionPane.showOptionDialog(null,
InputDialog,
"Login",
JOptionPane.DEFAULT_OPTION,
JOptionPane.PLAIN_MESSAGE,
null,
buttons,
buttons[0]);
System.out.println(option); //Check
if (option == JOptionPane.CLOSED_OPTION ) {
return;
}
else if (option == 0) {
if (CheckAccount(username,password)) {
System.out.println("Logged in");
} else {
System.out.println("Wrong Password/Username");
}
} else if (option == 1) {
System.out.println("Create Account.");
}
} while (!(CheckAccount(username,password)));
}
private static boolean CheckAccount(String username, String password) {
for (int i=0; i>NoOfUsers; i++) {
if ((username == Accounts[i][0]) && (password == Accounts[i][1])) {
return true;
}
}
return false;
}
}
In "main", I called the Login() method, and Eclipse is forcing me to put the word "static" in front of the method name.
Is there anyway I can modify the program so that the line can be written as:
private void Login() {...};
private boolean CheckAccount(...) {...} etc.?
[Extra question:
Because of the word "static", I can't put something like the word "public" before String[][] Accounts = new String[NoOfUsersL][2];
Which makes the Accounts array can't be accessed by CheckAccount.
How can I modify the program to fix this problem as well.]
Thx everyone in advance.