标记"}"上的 Eclipse 语法错误,删除此标记和语法错误,插入"}"以完成类体



我正在尝试为书店店员制作一个练习程序,允许店员在其数据库中添加、删除、编辑和搜索书籍。我几乎制作了整个程序,但是我遇到了 2 个错误。总共有 234 行代码,所以我会尝试将其缩短到相关部分,以使那些愿意帮助我的人更容易。我正在使用 Eclipse 和 JDE 和 JDK 10。据我所知,Eclipse 项目是使用 JavaSE-10 执行环境启动的。以下是导致错误的 2 种方法。

public class Bookstore {
public static void main(String[] args) {
try(
//Creating table connection and statement
Connection conn = DriverManager.getConnection("***********",
"****", "*********"); //Please note that I blocked out the actual connection information here
Statement stmt = conn.createStatement();
){
Scanner input = new Scanner(System.in);
int selection = 0;
//Menu for action selection and user input
while(selection != 5) {
System.out.println("Please enter the number corresponding to the action you would like to take:n"
+ "1. Enter bookn"
+ "2. Update bookn"
+ "3. Delete bookn"
+ "4. Search booksn"
+ "5. Exit");
selection = input.nextInt();
//Selection sorting
if(selection == 1) {
//Collecting book information
System.out.println("Please enter the Title of the book you would like to put into the system: ");
String title = input.next();
System.out.println("Please enter the Author of said book: ");
String author = input.next();
System.out.println("Please enter the number of said book currently in stock: ");
int qty = input.nextInt();
//Sending info to the addBook method
addBook(title, author, qty, stmt);
} else if(selection == 2) {
//Collecting book information
System.out.println("Please enter the id of the book you would like to update: ");
int id = input.nextInt();
//Sending info to the updateBook method
updateBook(id, stmt);
} else if(selection == 3) {
//Collecting book information
System.out.print("Please enter the id of the book you would like to delete from the system: ");
int id = input.nextInt();
//Sending info to deleteBook method
deleteBook(id, stmt);
} else if(selection == 4) {
searchStore(stmt);
} else if(selection == 5) {
System.out.println("Goodbye");
input.close();
} else { //Invalid entry handler
System.out.println("Sorry, that isn't a valid selection.");
}
}
} catch(SQLException ex) {
ex.printStackTrace();
}
}
} //This is the line giving me the error "Syntax error on token "}", delete this token"

现在我已经对这段代码块底部的错误做了一些研究。据我所知,我没有缺少任何括号,并且在类之外没有创建任何会导致此错误的变量或任何内容。我能找到的唯一另一个解决方案是"Eclipse只是很奇怪"。

我的第二个错误来自这段代码:

public static void resultSetPrinter(ResultSet rset) {
while(rset.next()) {
String title = rset.getString("Title");
String author = rset.getString("Author");
int qty = rset.getInt("qty");
System.out.println("Title: " + title + "nAuthor: " + author + "nNumber in stock: " + qty + "nn");
}
if(rset == null) {
System.out.println("No records for the entry could be found.");
}
} //This is the line giving me the "Syntax error, insert "}" to complete ClassBody" error

我还对此块底部的错误进行了一些研究,当我按照要求删除括号时,错误会跳到此块之前的方法。我没有在类中包含其他 4 个方法来尝试减少运行所有这些代码的头痛,因为它们不会给我错误。在这一点上,任何帮助将不胜感激,我完全被难住了。

主要感谢Elliott Frisch,我找到了答案。基本上,我需要将所有方法都以Bookstore的名义放入我的主类中。我将}移到程序的末尾,并为每个方法添加了 try catch 语句。例如,我将问题中的最后一个代码块更改为:

public static void resultSetPrinter(ResultSet rset) {
try {
if(rset.next()) {
while(rset.next()) {
String title = rset.getString("Title");
String author = rset.getString("Author");
int qty = rset.getInt("Qty");
System.out.println("Title: " + title + "nAuthor: " + author + "nNumber in stock: " + qty + "n");
}
} else {
System.out.println("No records for the entry could be found.n");
}
} catch(SQLException ex) {
ex.printStackTrace();
}
}

您还会注意到,我添加了一个 if else 语句来检查 ResultSetrset是否为空,如果不是,我照常进行,如果是,我打印了一条简单的消息,让用户知道没有找到任何内容。

感谢Elliott Frisch和Marco13的协助。

最新更新