为什么我的 Telnet 程序的一部分会抛出有关放错位置的“}”的错误?
Why is part of my Telnet program throwing an error about misplaced '}'?
我正在使用 BBEdit,它似乎认为每个 '}' 都放在 "hello" || 之后"hi" if 语句不匹配,这显然不是真的。终端也这么认为,因为它会抛出一个关于 'try' 没有 'catch' 的错误。我想知道是否有人遇到过同样的问题 and/or 知道解决方案吗?
提前致谢!
class client_handler extends Thread {
private Socket conn;
client_handler(Socket conn) {
this.conn = conn;
}
public void run() {
String line, input="";
String username;
String password;
String UID;
Map<String, String> database_UID = new HashMap<String, String>();
database_UID.put("admin", "1234");
try {
//Fetches the socket reading and writing streams
DataInputStream in = new DataInputStream(conn.getInputStream());
PrintStream out = new PrintStream(conn.getOutputStream());
//Sends a welcome message to new client
out.println("Welcome to the server\nType your username and password like:\nUsername_Password");
//Begins to read input from the client
while((line = in.readLine()) != null && !line.equals(".")) {
//The server replies with the same message
if(line.equalsIgnoreCase("server_quit")) {
//Tells the server the command was given
System.out.println("Given command 'server_quit', closing client connection at "+conn.getPort());
//Tells the user that the server is closing
out.println("Quitting the server...\nHave a nice day!");
conn.close();
}
if(line.equals("admin_"+database_UID.get("admin") {
out.println("Welcome back, Admin!");
}
if(line.equalsIgnoreCase("hello") || line.equalsIgnoreCase("hi")) {
out.println("Good day!");
}
}
System.out.println("Client terminated at "+conn.getPort());
conn.close();
}
catch(IOException e) {
System.out.println("IOException on socket: "+e);
e.printStackTrace();
}
}
看上面一行的括号:
if(line.equals("admin_"+database_UID.get("admin") {
3开,1关。那是不对的。检查这类东西(括号,大括号)时从零开始。开盘加一,收盘减一。如果你最终没有得到零,你就错过了一些(或者如果你最终为负,则添加了一些)。
if (line.equals("admin_" + database_UID.get("admin"))) {
最后还有一个}
试试这个:
if (line.equals("admin_" + database_UID.get("admin"))) {
而不是
if(line.equals("admin_"+database_UID.get("admin") {
我正在使用 BBEdit,它似乎认为每个 '}' 都放在 "hello" || 之后"hi" if 语句不匹配,这显然不是真的。终端也这么认为,因为它会抛出一个关于 'try' 没有 'catch' 的错误。我想知道是否有人遇到过同样的问题 and/or 知道解决方案吗?
提前致谢!
class client_handler extends Thread {
private Socket conn;
client_handler(Socket conn) {
this.conn = conn;
}
public void run() {
String line, input="";
String username;
String password;
String UID;
Map<String, String> database_UID = new HashMap<String, String>();
database_UID.put("admin", "1234");
try {
//Fetches the socket reading and writing streams
DataInputStream in = new DataInputStream(conn.getInputStream());
PrintStream out = new PrintStream(conn.getOutputStream());
//Sends a welcome message to new client
out.println("Welcome to the server\nType your username and password like:\nUsername_Password");
//Begins to read input from the client
while((line = in.readLine()) != null && !line.equals(".")) {
//The server replies with the same message
if(line.equalsIgnoreCase("server_quit")) {
//Tells the server the command was given
System.out.println("Given command 'server_quit', closing client connection at "+conn.getPort());
//Tells the user that the server is closing
out.println("Quitting the server...\nHave a nice day!");
conn.close();
}
if(line.equals("admin_"+database_UID.get("admin") {
out.println("Welcome back, Admin!");
}
if(line.equalsIgnoreCase("hello") || line.equalsIgnoreCase("hi")) {
out.println("Good day!");
}
}
System.out.println("Client terminated at "+conn.getPort());
conn.close();
}
catch(IOException e) {
System.out.println("IOException on socket: "+e);
e.printStackTrace();
}
}
看上面一行的括号:
if(line.equals("admin_"+database_UID.get("admin") {
3开,1关。那是不对的。检查这类东西(括号,大括号)时从零开始。开盘加一,收盘减一。如果你最终没有得到零,你就错过了一些(或者如果你最终为负,则添加了一些)。
if (line.equals("admin_" + database_UID.get("admin"))) {
最后还有一个}
试试这个:
if (line.equals("admin_" + database_UID.get("admin"))) {
而不是
if(line.equals("admin_"+database_UID.get("admin") {