0

Query by loan ID

// Method that allows the user to query by loan ID

public static void QuerybyloanID(List<Loan> data) {
    try (Scanner entry = new Scanner(System.in)) {
        System.out.print("Please, enter the loan ID: ");
        String userentry = entry.next();

// For loop is used to locate the loan ID within the list of loans

        for (int i = 0; i < data.size(); i++) {
            String loanID = data.get(i).getLoanID();
            
            if (loanID.equals(userentry)) {
                boolean pass = true;
                String loanPassword = data.get(i).getPasscode();

                // Three possible character from the loan password
                int one;
                int two;
                int three;

                int [] randomCharacters = randomUniqueGenerator(3, loanPassword.length());
                
                one = randomCharacters[0];
                two = randomCharacters[1];
                three = randomCharacters[2];

// Check to see if the random numbers are the same or different. -> Implement a Hash Map.

                char entry_one = loanPassword.charAt(one);
                char entry_two = loanPassword.charAt(two);
                char entry_three = loanPassword.charAt(three);

                String entry_one_modify = Character.toString(entry_one);
                String entry_two_modify = Character.toString(entry_two);
                String entry_three_modify = Character.toString(entry_three);

                String [] entriesmain = {entry_one_modify, entry_two_modify, entry_three_modify};
                
                try (Scanner entrytwo = new Scanner(System.in)) {
                    for (int k = 0; k < 3; k ++) {
                        System.out.print("Enter character " + randomCharacters[k] + " of the loan pass code: ");
                        String userentrytwo = entrytwo.next();
                        entrytwo.nextLine();

                        if (userentrytwo.equals(entriesmain[k])) {
                            continue;
                        } else {
                            pass = false;
                            continue;
                        }
                    }
                }
                

// Check to see if pass is true or false then take the appropriate action

                if (pass) {
                    System.out.println("Customer Name: " + data.get(i).getFirstName() + " " + data.get(i).getLastName());
                    System.out.println("Branch Code: " + data.get(i).getBrachCode());
                    System.out.println("Gender: " + data.get(i).getGender());
                    System.out.println("Date of Birth: " + data.get(i).getDOB());
                    System.out.println("Loan Amount: " + data.get(i).getLoanAmount());
                    System.out.println("Customer Phone Number: " + data.get(i).getPhoneNumber());
                    MainMenu.options();
                } else { 
                    System.out.println("Wrong Passcode !"); 
                    MainMenu.options();
                } 


            }
foodsr
  • 11
  • 1
  • 1
    Does this answer your question? [Scanner NoSuchElementException](https://stackoverflow.com/questions/15443383/scanner-nosuchelementexception) – Thomas Kläger Feb 09 '22 at 21:06
  • 1
    Your `QuerybyloanID(List data)` creates a `Scanner` over `System.in` and closes it, thereby closing `System.in` too. After that you can no longer read anything from `System.in`, not even in the Scanner that you created in the main method. It's better to create only one `new Scanner(System.in)` and either pass it around or store it in a global variable. – Thomas Kläger Feb 09 '22 at 21:08
  • @foodsr, don't feel bad. This is a very common mistake made by many; even experienced developers. Thomas Kläger's comment is correct. – hfontanez Feb 09 '22 at 21:12
  • 1
    @ThomasKlager. Thanks for the support I've been able to resolve the issue. – foodsr Feb 13 '22 at 18:17

0 Answers0