0
package assignment8;
import java.util.Scanner;

public class BankAcc {

    static double balance[] = new double[100];
    static double withdraw = 0.0 d;
    static double deposit = 0.0 d;
    static int accno[] = new int[100];
    static char acctype[] = new char[100];

    static void input(int a) {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < a; i++) {
            System.out.println("Enter your account number: ");
            accno[i] = sc.nextInt();
            System.out.println("Enter your account type: ");
            acctype[i] = sc.next().charAt(0);
            System.out.println("Enter your balance: ");
            balance[i] = sc.nextDouble();
        }
        sc.close();

    }

    static void displayall(int b) {
        for (int i = 0; i < b; i++) {
            System.out.println("Account number: " + accno[i]);
            System.out.println("Account type: " + acctype[i]);
            System.out.println("Balance: " + balance[i]);
        }
    }

    static void displaysome(int c) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your account number: ");
        int num = sc.nextInt();
        boolean flag = false;

        for (int i = 0; i < c; i++) {
            if (num == accno[i]) {
                flag = true;
            } else {
                throw new ArrayIndexOutOfBoundsException("Account Number not found!");
            }
        }

        for (int i = 0; i < c; i++) {
            if (flag == true) {
                System.out.println("Account number: " + accno[num]);
                System.out.println("Account type: " + acctype[num]);
                System.out.println("Balance: " + balance[num]);
            }
        }

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter number of account holders: ");
        int n = sc.nextInt();
        BankAcc obj = new BankAcc();
        Savings sav = new Savings();
        Current cur = new Current();

        input(n);

        int ch = 1;

        while (ch == 1) {
            int choice;
            System.out.println("Welcome to our bank Server!\n" +
                "What would you like to see?\n" +
                "1. Display all accounts\n" +
                "2. Display a single account\n" +
                "3. Withdraw money from your account\n" +
                "4. Deposit money in your account\n");
            choice = sc.nextInt();
            sc.close();
            switch (choice) {
                case 1:
                    displayall(n);
                    System.out.println("Would you like to continue? Enter 1 for YES");
                    ch = sc.nextInt();
                    break;

                case 2:
                    displaysome(n);
                    System.out.println("Would you like to continue? Enter 1 for YES");
                    ch = sc.nextInt();
                    break;

                case 3:
                    int num1;
                    System.out.println("Enter your account number: ");
                    num1 = sc.nextInt();

                    boolean flag1 = false;

                    for (int i = 0; i < n; i++) {
                        if (num1 == accno[i]) {
                            flag1 = true;
                        } else {
                            throw new ArrayIndexOutOfBoundsException("Account Number not found! Sorry.");
                        }
                    }

                    for (int i = 0; i < n; i++) {
                        if (flag1 == true) {
                            if (acctype[i] == 's') {
                                sav.withdraw(i, obj);
                            } else if (acctype[i] == 'c') {
                                cur.withdraw(i, obj);

                            }
                        }
                    }
                    System.out.println("Would you like to continue? Enter 1 for YES");
                    ch = sc.nextInt();
                    break;

                case 4:
                    int num2;
                    System.out.println("Enter your account number: ");
                    num2 = sc.nextInt();

                    boolean flag2 = false;

                    for (int i = 0; i < n; i++) {
                        if (num2 == accno[i]) {
                            flag2 = true;
                        } else {
                            throw new ArrayIndexOutOfBoundsException("Account Number not found! Sorry.");
                        }
                    }

                    for (int i = 0; i < n; i++) {
                        if (flag2 == true) {
                            System.out.println("Enter the amount you want to deposit: ");
                            int deposit = sc.nextInt();
                            balance[i] = balance[i] + deposit;
                            System.out.println("Current balance: " + balance[i]);
                        } else {
                            throw new ArrayIndexOutOfBoundsException("Amount not deposited.");
                        }
                    }
                    break;
            }
        }
    }
}

class Savings extends BankAcc {
    void withdraw(int i, BankAcc obj) {
        Scanner sav = new Scanner(System.in);
        System.out.println("Enter the amount you wish to withdraw: ");
        int withdraw = sav.nextInt();
        if (withdraw <= balance[i]) {
            balance[i] -= withdraw;

            System.out.println("Current balance: " + balance[i]);
        } else {

            throw new ArithmeticException("Insufficient balance!");
        }

    }
}

class Current extends BankAcc {
    void withdraw(int i, BankAcc obj) {
        Scanner cur = new Scanner(System.in);
        System.out.println("Enter the amount you wish to withdraw: ");
        int withdraw = cur.nextInt();
        balance[i] -= withdraw;
        System.out.println("Current balance: " + balance[i]);
        cur.close();
    }

}

I have been trying to execute this code, but despite there being no errors in the actual code, I am getting a NoSuchElementException for it. It's pointing me to a line where I can't understand what is wrong.

Exception in thread "main" java.util.NoSuchElementException
    at java.base/java.util.Scanner.throwFor(Scanner.java:937)
    at java.base/java.util.Scanner.next(Scanner.java:1594)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
    at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
    at assignment8/assignment8.BankAcc.main(BankAcc.java:79)

This is the error it's giving.

I also checked all the suggestions from this link: Java NoSuchElementException But I am still getting the same errors.

chameerar
  • 322
  • 1
  • 2
  • 8
JDANDE
  • 1
  • 1
  • 1
    1. Don't close any Scanner based on `System.in` until you are fully done using `System.in`, since once it is closed, it cannot be reopened again. 2. Best to have only one Scanner based on `System..in` and then pass it to where it is needed in your program. 3. Always [search this site on your error first](https://www.google.com/search?q=site%3Astackoverflow.com+java+scanner+NoSuchElementException) to inspect similar questions before posting a new question. – Hovercraft Full Of Eels Jun 10 '23 at 17:01

0 Answers0