I’m not able to create array with keys in Java

Asked

Viewed 57 times

1

I am studying the Java language and to practice I am creating a simple login program. I have a class called Database that should save an array with the information (name and password) of the users.

The problem is that when trying to compile the code, the compiler points to an error where the array is being initialized. See below the code and the error:

public class Database{

    private static String[][] data = new String[5][2];

    {
        Database.data[0] = {"Jean", "pato_34"};
        Database.data[1] = {"Maria", "lolzin"};
        Database.data[2] = {"Lucas", "bot3000"};
        Database.data[3] = {"Pedro", "showd3b0la"};
    }

    public static String[] find(String username){

        for (String[] user : Database.data){

            if (username == user[0]){
                return user;
            }
        }

        return {"", ""};
    }
}

.\server\Database.java:8: error: illegal start of expression
        Database.data[0] = {"Jean", "pato_34"};
                           ^
.\server\Database.java:8: error: not a statement
        Database.data[0] = {"Jean", "pato_34"};
                            ^
.\server\Database.java:8: error: ';' expected
        Database.data[0] = {"Jean", "pato_34"};
                                  ^
.\server\Database.java:9: error: ']' expected
        Database.data[1] = {"Maria", "lolzin"};
                      ^
.\server\Database.java:9: error: ';' expected
        Database.data[1] = {"Maria", "lolzin"};
                       ^
.\server\Database.java:9: error: illegal start of type
        Database.data[1] = {"Maria", "lolzin"};
                         ^
.\server\Database.java:9: error: <identifier> expected
        Database.data[1] = {"Maria", "lolzin"};
                          ^
.\server\Database.java:9: error: ';' expected
        Database.data[1] = {"Maria", "lolzin"};
                            ^
.\server\Database.java:9: error: illegal start of type
        Database.data[1] = {"Maria", "lolzin"};
                                   ^
.\server\Database.java:9: error: <identifier> expected
        Database.data[1] = {"Maria", "lolzin"};
                                    ^
.\server\Database.java:9: error: ';' expected
        Database.data[1] = {"Maria", "lolzin"};
                                             ^
.\server\Database.java:10: error: ']' expected
        Database.data[2] = {"Lucas", "bot3000"};
                      ^
.\server\Database.java:10: error: ';' expected
        Database.data[2] = {"Lucas", "bot3000"};
                       ^
.\server\Database.java:10: error: illegal start of type
        Database.data[2] = {"Lucas", "bot3000"};
                         ^
.\server\Database.java:10: error: <identifier> expected
        Database.data[2] = {"Lucas", "bot3000"};
                          ^
.\server\Database.java:10: error: ';' expected
        Database.data[2] = {"Lucas", "bot3000"};
                            ^
.\server\Database.java:10: error: illegal start of type
        Database.data[2] = {"Lucas", "bot3000"};
                                   ^
.\server\Database.java:10: error: <identifier> expected
        Database.data[2] = {"Lucas", "bot3000"};
                                    ^
.\server\Database.java:10: error: ';' expected
        Database.data[2] = {"Lucas", "bot3000"};
                                              ^
.\server\Database.java:11: error: ']' expected
        Database.data[3] = {"Pedro", "showd3b0la"};
                      ^
.\server\Database.java:11: error: ';' expected
        Database.data[3] = {"Pedro", "showd3b0la"};
                       ^
.\server\Database.java:11: error: illegal start of type
        Database.data[3] = {"Pedro", "showd3b0la"};
                         ^
.\server\Database.java:11: error: <identifier> expected
        Database.data[3] = {"Pedro", "showd3b0la"};
                          ^
.\server\Database.java:11: error: ';' expected
        Database.data[3] = {"Pedro", "showd3b0la"};
                            ^
.\server\Database.java:11: error: illegal start of type
        Database.data[3] = {"Pedro", "showd3b0la"};
                                   ^
.\server\Database.java:11: error: <identifier> expected
        Database.data[3] = {"Pedro", "showd3b0la"};
                                    ^
.\server\Database.java:11: error: ';' expected
        Database.data[3] = {"Pedro", "showd3b0la"};
                                                 ^
.\server\Database.java:14: error: class, interface, or enum expected
    public static String[] find(String username){
                  ^
.\server\Database.java:20: error: class, interface, or enum expected
            }
            ^
.\server\Database.java:24: error: class, interface, or enum expected
    }
    ^
.\server\Database.java:9: error: cannot find symbol
        Database.data[1] = {"Maria", "lolzin"};
                ^
  symbol:   class data
  location: class Database
.\server\Database.java:10: error: cannot find symbol
        Database.data[2] = {"Lucas", "bot3000"};
                ^
  symbol:   class data
  location: class Database
.\server\Database.java:11: error: cannot find symbol
        Database.data[3] = {"Pedro", "showd3b0la"};
                ^
  symbol:   class data
  location: class Database
Main.java:6: error: Client(String,String) has private access in Client
        Client client = new Client("Jean", "pato_34");
                        ^
.\server\Session.java:15: error: cannot find symbol
        String[] user = Database.find(username);
                                ^
  symbol:   method find(String)
  location: class Database
35 errors

What am I doing wrong? I know it’s something related to syntax but I can’t understand where the problem is.

1 answer

1


The creation of Java arrays is a bit boring.

This way you tried only works if it is in the variable’s own declaration. Ex:

String[] nomes = {"fulano", "ciclano"};

But if I do it in another line, it’s a mistake:

String[] nomes;
nomes = {"fulano", "ciclano"}; // erro!

In that case it only works if you use new:

String[] nomes;
nomes = new String[] {"fulano", "ciclano"};

So in your case it should be:

Database.data[0] = new String[] {"Jean", "pato_34"};

Or create all the values at once:

private static String[][] data = {
    {"Jean", "pato_34"},
    {"Maria", "lolzin"},
    // etc...
 };

Another unrelated detail is that in Java Strings should be compared with equals instead of ==:

if (username.equals(user[0]))

Browser other questions tagged

You are not signed in. Login or sign up in order to post.