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.
Thank hkotsubo <3
– JeanExtreme002