这个有点奇怪。我读过很多关于NoSuchElementException的帖子:找不到行,它通常似乎与扫描仪对象被关闭或没有更多行要读取有关。然而,我的问题是,只有当我在Eclipse中单击红色方块停止程序时,才会出现此错误。我可以启动我的程序,然后在它到达第一个用户提示时立即单击停止,不输入任何内容,一旦我单击停止,它就会给我错误。这个程序一直运行良好,直到我按下停止按钮,这是最奇怪的事情。这从来都不是问题。我已经为这个项目工作了几个星期,直到今天才发生过这样的事情。我确实从Eclipse 2020-06更新到了Eclipse 2020-12,因为我试图让STS 4作为插件工作(另一个问题),所以我想知道这是否与新版Eclipse有关。直到今天更新后,我才注意到这种情况开始发生。我对代码进行了双重、三重、四重检查,并尝试注释掉不同的部分,看看是某个循环还是导致它的原因,但无论我注释掉哪个部分,错误都会持续存在。我已经检查过我的扫描程序没有提前关闭,我没有创建多个扫描程序实例,行被正确读取等。整个代码都在我的github上:
https://github.com/ksinelli/OnlineBanking/tree/master/OnlineBanking
它从LoginScreen类开始,唯一的其他相关代码将在DatabaseConnection类和MyScanner类中,考虑到我甚至不需要在程序中深入到其他类就可以给出此异常。
对于那些不想查看github的人来说,LoginScreen类是这样的:
package HomePage;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import Customer.Customer;
import Customer.CustomerDashboard;
import Utility.MyScanner;
import Utility.DatabaseConnection;
public class LoginScreen {
private static ResultSet resultSet;
private static PreparedStatement stmt;
public static void main(String[] args) {
try {
DatabaseConnection.getDbConnection();
MyScanner.openScanner();
System.out.println("Hello, and welcome to KAS Financial!\n");
startup();
}
catch (Exception e){
e.printStackTrace();
}
}
public static void startup() {
Customer customer = new Customer();
boolean isRunning = true;
while (isRunning == true) {
System.out.println("Type \"Sign in\" to access your account.");
System.out.println("Type \"Create account\" to create a new account.");
System.out.println("Type \"Reset password\" to change your password.\n");
System.out.println("Type \"Home\" at any time to return to this menu.\n");
System.out.println("Pro tip: capitalization doesn't matter when typing your choice :)");
String choice = MyScanner.getInputToLower();
switch (choice) {
case "sign in":
isRunning = false;
signIn(customer);
break;
case "create account":
isRunning = false;
createAccount(customer);
break;
case "reset password":
isRunning = false;
resetPassword(customer);
break;
default:
System.out.println("I'm sorry, I didn't understand that. Please try again.\n");
}
}
}
public static void signIn(Customer customer) {
customer.setUsername(getUsernameFromDb(customer));
customer = getPasswordFromDb(customer);
CustomerDashboard.dashboardMenu(customer);
}
public static void resetPassword(Customer customer) {
customer.setUsername(getUsernameFromDb(customer));
customer = getPasswordFromDb(customer);
updatePassword(customer);
}
//retrieve entered username from database and if it does not exist, redirect to home page.
public static String getUsernameFromDb(Customer customer) {
System.out.println("Please enter your username.");
String input = MyScanner.getInputToLower();
try {
stmt = DatabaseConnection.prepareStatement("select username from customer where username = ?");
stmt.setString(1, input);
resultSet = stmt.executeQuery();
if (resultSet.next() == false) {
System.out.println("That username does not exist in our system.\n");
startup();
}
}
catch (SQLException e) {
e.printStackTrace();
}
return input;
}
//retrieve database password and compare to entered password. If they do not match, return to home page.
public static Customer getPasswordFromDb(Customer customer) {
System.out.println("Please enter your password.");
String password = MyScanner.getInput();
String dbPassword;
try {
stmt = DatabaseConnection.prepareStatement("select password from customer where username = ?");
stmt.setString(1, customer.getUsername());
resultSet = stmt.executeQuery();
resultSet.next();
dbPassword = resultSet.getString("password");
if (!password.equals(dbPassword)) {
System.out.println("The password you entered does not match our records. Please try again.\n");
startup();
}
customer.pullProfileFromDatabase(customer);
}
catch (SQLException e) {
e.printStackTrace();
}
return customer;
}
//Update SQL database with new user-supplied password
public static void updatePassword(Customer customer) {
System.out.println("Please enter a new password.");
String newPassword = MyScanner.getInput();
try {
stmt = DatabaseConnection.prepareStatement("Update customer set password = ? where username = ?");
stmt.setString(1, newPassword);
stmt.setString(2, customer.getUsername());
stmt.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
System.out.println("Your password has been updated. Please login again with your new credentials.\n");
signIn(customer);
}
//create new customer profile, assign it to customer object, and insert it into database
public static void createAccount(Customer customer) {
boolean usernameIsValid = false;
while (usernameIsValid == false) {
System.out.println("Please enter a username.");
customer.setUsername(MyScanner.getInputToLower());
try {
stmt = DatabaseConnection.prepareStatement("select username from customer where username = ?");
stmt.setString(1, customer.getUsername());
resultSet = stmt.executeQuery();
if (resultSet.next()) {
System.out.println("That username already exists.\n");
}
else {
usernameIsValid = true;
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("Please create a password for your account.");
customer.setPassword(MyScanner.getInput());
try {
stmt = DatabaseConnection.prepareStatement("Insert into customer (username, password) values (?,?)");
stmt.setString(1, customer.getUsername());
stmt.setString(2, customer.getPassword());
stmt.executeUpdate();
}
catch (SQLException e) {
e.printStackTrace();
}
customer.createOrUpdateProfile(customer);
customer.pushProfileToDatabase(customer);
System.out.println("Your account has been created. Please login using your new credentials.\n");
signIn(customer);
}
}
DatabaseConnection类是这样的
package Utility;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class DatabaseConnection {
static Connection conn;
public static void getDbConnection() {
try {
conn = DriverManager.getConnection(connection deets here);
}
catch (SQLException e) {
e.printStackTrace();
}
}
public static void closeDbConnection() {
try {
conn.close();
}
catch (SQLException e) {
e.printStackTrace();
}
}
public static PreparedStatement prepareStatement(String SQL) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(SQL);
return stmt;
}
}
MyScanner类是这样的
package Utility;
import java.util.Scanner;
import HomePage.LoginScreen;
public class MyScanner {
public static Scanner scan;
public static void openScanner() {
scan = new Scanner(System.in);
}
public static void closeScanner() {
scan.close();
}
public static String getInput() {
String input = scan.nextLine();
if (input.toLowerCase().equals("home")) {
LoginScreen.startup();
}
return input;
}
public static String getInputToLower() {
String input = scan.nextLine().toLowerCase();
if (input.equals("home")) {
LoginScreen.startup();
}
return input;
}
public static int getNumber() {
int input = scan.nextInt();
scan.nextLine();
return input;
}
}
有什么想法吗?
有人可以打开这个备份吗?我看不出这是怎么回事。我在互联网上搜索过,还没有遇到任何关于在手动停止程序后才出现异常的线索——任何异常。
更新:
这似乎是Eclipse 2020-12或JRE 14的问题。我重新安装了Eclipse 2020-06,这是我最初使用的,直到出现此错误。我将代码复制并粘贴到一个新项目中,一切运行良好——停止程序后没有错误。我在运行配置中注意到JRE设置为Java SE 11。然而,在2020-12版本中,它被设置为Java SE 14。我不确定如何正确地更改它,所以我不知道这是否正是导致错误的原因(正如我所说的,我在这方面还是个新手)。但谢谢你结束我的问题,告诉我这是一个重复的问题,而你可能甚至没有读过整件事。