1

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.

DeeThreeCay
  • 45
  • 1
  • 5
  • 2
    You have to create an instance of `Main` in order to use methods as instance methods. – Mr. Polywhirl Mar 18 '16 at 11:45
  • It's forcing you to use `static` because non-static methods belong to an `instance` and not a `class`. You'll have to create an instance of `Main`. – Drew Kennedy Mar 18 '16 at 11:46
  • answer for extra question : Yes,You can.Static variables are also known as class variables. Local variables cannot be declared static. – Spartan Mar 18 '16 at 11:56

4 Answers4

1

static methods could be call static methods(without an instance) so to call an instance method first create it and call from instance as exampled like below

public static void main(String[] args){
    Main main = new Main();    
    main.login();
}

private void login() { //remove static from instance methods
Yusuf K.
  • 4,195
  • 1
  • 33
  • 69
  • Wow did not know it was that easy, thx so much :D – DeeThreeCay Mar 18 '16 at 11:48
  • Why do you store the instance of the Main in a variable? Yoh waste memory. – Bálint Mar 18 '16 at 11:48
  • 1
    @Bálint Memory is taken with `new Main()`. holding it in a variable is not very important as you point. This variable is simply a reference to it. – Yusuf K. Mar 18 '16 at 11:52
  • @DeeThreeCay You're welcome. Do not forget calling `setVisible` method to show JFrame and also you need to use `setBounds` to set size and position of frame – Yusuf K. Mar 18 '16 at 11:55
0

Do a new Main().login();

You should follow java naming conventions, variable and method names should start with a lowercase letter.

Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
Bálint
  • 4,009
  • 2
  • 16
  • 27
0

Use:

public static void main(String[] args){
    Main main = new Main();
    main.login();
}
Puce
  • 37,247
  • 13
  • 80
  • 152
0

In order to remove static qualifiers, you must make all your methods and variables instance variables and methods. Then you can instantiate your JFrame object and call login().

I also converted methods/variables names to mixedCase as per Java offical conventions. I take it that you started programming in VB or C#?

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Main extends JFrame {
    private static final long serialVersionUID = -105943237549003486L;

    private int numOfUsers;
    private String[][] accounts;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Main m = new Main();

                m.login("F:/TextFiles/loginaccs.txt");
            }
        });
    }

    // Constructor, set up instance values here.
    public Main() {
        super();
    }

    private void login(final String fileName) {
        try {
            BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));

            numOfUsers = Integer.parseInt(file.readLine());
            accounts = new String[numOfUsers][2];

            for (int i = 0; i < numOfUsers; i++) {
                accounts[i][0] = file.readLine();
                accounts[i][1] = file.readLine();
            }

            for (int i = 0; i < numOfUsers; 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 boolean checkAccount(String username, String password) {
        for (int i = 0; i > numOfUsers; i++) {
            if (username == accounts[i][0] && password == accounts[i][1]) {
                return true;
            }
        }

        return false;
    }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132