My initial run of main
works fine, but when it is returned from creating a new account it will throw a NoSuchElementException
for line 21, which is nextInt()
.
My main looks like this:
package BankApp;
import java.util.Scanner;
import java.io.IOException;
public class App {
public static void main(String[] args) throws IOException {
String fname;
String sname;
String password;
long accNum;
int appChoice = 0;
while(true){
System.out.println("******Welcome to Bank******");
System.out.println("");
System.out.println("Do you have an account with us?");
System.out.println("(1) Yes");
System.out.println("(2) No");
System.out.print("Enter Input: ");
Scanner sc = new Scanner(System.in);
appChoice = sc.nextInt();
sc.nextLine();
try {
switch(appChoice){
case 2:
try {
System.out.println("Create New Account");
System.out.println("Enter Full name");
System.out.print("First name: ");
fname = sc.nextLine();
System.out.print("Surname: ");
sname = sc.nextLine();
System.out.print("Emter Password: ");
password = sc.nextLine();
if (BankManagement.createAccount(fname, sname, password)){
System.out.println("Account Created Successfully");
} else {
System.out.println("ERROR: Acount Creation Failed");
}
} catch (Exception e) {
System.out.println("Error: Valid Data :: Insertion Error");
}
break;
case 1:
try {
System.out.println("Account Login");
System.out.print("Enter Account Number: ");
accNum = sc.nextLong();
sc.nextLine();
System.out.print("Enter Password: ");
password = sc.nextLine();
if (BankManagement.loginAccount(accNum, password)){
System.out.println("Logoin Successful");
} else {
System.out.println("ERROR: Login Failed");
}
} catch (Exception e){
System.out.println("ERROR: Valid Data :: Login Failed");
}
break;
default:
System.out.println("Invalid Input");
}
if (appChoice == 5){
System.out.println("Exited Successfully");
break;
}
} catch (Exception e){
System.out.println("Enter Valid Entry");
}
sc.close();
}
}
}
and my createAccount
looks like this:
package BankApp;
import java.util.Scanner;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.Statement;
public class BankManagement {
private static final String NULL = null;
private static long accnumber = Math.round(1 + Math.random() * 100000000);
static Connection con = sqlCon.getConnection();
static String sql = "";
public static boolean createAccount(String fname, String sname, String password){
try{
// validating new acc details
if (fname.equals("") || "".equals(sname) || password.equals(NULL)) {
System.out.println("All Fields Required!");
return false;
}
// query
Statement st = con.createStatement();
sql = "INSERT INTO customer(first_name, surname, balance, password, account_number, created_on, last_login) values ('" + fname + "', '" + sname +"','1000', '"+ password +"', '" + accnumber +"', NOW(), NOW())" ;
if (st.executeUpdate(sql) == 1){
PreparedStatement accDetails = con.prepareStatement("SELECT * FROM customer WHERE first_name = ? AND surname= ? AND password = ?");
accDetails.setString(1, fname);
accDetails.setString(2, sname);
accDetails.setString(3, password);
ResultSet rs = accDetails.executeQuery();
rs.next();
System.out.println("Your account number is: " + rs.getInt("account_number"));
return true;
}
}
catch (SQLIntegrityConstraintViolationException e){
System.out.println("Username not available!");
}
catch (Exception e) {
e.printStackTrace();
}
return false;
}
I've tried putting it into a try-catch with hasNextInt()
and refreshing the input with next()
, as well as changing Scanner
to BufferedReader
. If I move sc.nextInt()
to the uder the try
it just infinite loops, same with using BufferedReader
.