简体   繁体   中英

Scanner input with exception handling in a try/catch is not working like intended

I am making an atm program that takes in an account number and a pin. When I try to type in anything except the accepted accounts and pins that are predetermined, it is supposed to give an error message and the user tries again. The only problem is that it prints out the error message but moves on to the next input instead of repeating the previous input. ie goes from entering the account number to entering the pin number. The same thing happens for the pin as well. I really don't know what to make of it. Thanks in advance! Here's a snippet of my code:

import java.util.*;
import java.lang.*;

public class Menu {

   Scanner input = new Scanner(System.in);
   //Bank bank = new Bank();
   boolean keepGoing = true;

   public static void main(String[] args){
     Menu menu = new Menu();
     menu.startMenu();

   }//end main
   
   public void startMenu(){
    int choice = 0;
    int verify1 = 0;
    int verify2 = 0;
    int account = 0;
    int pin = 0;
    printGreet();
    while(keepGoing){
    System.out.print("Please enter Account number: ");
     do{
     try{
      account = input.nextInt();
      }//end try
     catch(NumberFormatException e){
      System.out.print("ERROR: That is not a valid account number. Please enter a valid account number (#####): ");
      input.nextLine();//clear buffer
      }//end catch
     catch(InputMismatchException e){
      System.out.print("Error: That is not a valid account number. Please enter a valid account number (#####): ");
      input.nextLine();//clear buffer 
      }//end catch
     if(account < 0 || account > 99999){
      System.out.print("ERROR: That is not a valid account number. Please enter a valid account number (#####): ");
      //input.nextLine();//clear buffer
      }//end if
     }while(!(account >= 0 && account <= 99999));//end do/while
     System.out.print("Please enter PIN number: ");
     do{
     try{
      pin = input.nextInt();
      }//end try
     catch(NumberFormatException e){
     System.out.print("ERROR: That is not a valid pin number. Please enter a valid pin number (#####): ");
     input.nextLine();//clear buffer
      }//end catch
     catch(InputMismatchException e){
      System.out.print("ERROR: That is not a valid pin number. Please enter a valid pin number (#####): ");
      input.nextLine();//clear buffer
      }//end catch
     if(pin < 0 || pin > 99999){
      System.out.print("ERROR: That is not a valid pin number. Please enter a valid pin number (#####): ");
      //input.nextLine();//clear buffer
      }//end if
    }while(!(pin >= 0 && pin <= 99999));//end do/while
    verify1 = verifyAccount(account);
    verify2 = verifyPin(pin);
    if((verify1 == 1) && (verify2 == 1)){
     printAdminMenu();
     choice = getAdminChoice();
     adminChoices(choice);
    }else if((verify1 == 2) && (verify2 == 2)){
     printUserMenu();
     choice = getUserChoice();
     userChoices(choice);
    }else{
     System.out.println("ERROR: YOU ARE NOT AN AUTHORIZED USER...GOODBYE");
     System.exit(0);
    }//end if/else
   }//end while
  }//end startMenu()

The whiles only check if the account number is invalid. When a parsing error occurs the account / pin variable is still 0 so it doesn't repeat.

I fixed/improved the code for you. Note you should always debug your code before you ask a question on StackOverflow.

public void startMenu() {
    int account = 0;
    int pin = 0;

    printGreet();

    while (keepGoing) {
        System.out.print("Please enter Account number: ");

        boolean invalid;

        do {
            invalid = false;

            try {
                account = input.nextInt();

                // Cancel if the number is invalid
                if (account < 0 || account > 99999) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException | InputMismatchException e) {
                System.out.print("ERROR: That is not a valid account number. Please enter a valid account number (#####): ");
                input.nextLine();

                // Mark the loop iteration as invalid
                invalid = true;
            }
        } while (invalid);

        System.out.print("Please enter PIN number: ");

        do {
            invalid = false;

            try {
                pin = input.nextInt();

                // Cancel if the number is invalid
                if (pin < 0 || pin > 99999) {
                    throw new NumberFormatException();
                }
            } catch (NumberFormatException | InputMismatchException e) {
                System.out.print("ERROR: That is not a valid pin number. Please enter a valid pin number (#####): ");
                input.nextLine();

                invalid = true;
            }
        } while (invalid);

        verify1 = verifyAccount(account);
        verify2 = verifyPin(pin);
        
        if ((verify1 == 1) && (verify2 == 1)) {
            printAdminMenu();
            choice = getAdminChoice();
            adminChoices(choice);
        } else if ((verify1 == 2) && (verify2 == 2)) {
            printUserMenu();
            choice = getUserChoice();
            userChoices(choice);
        } else {
            System.out.println("ERROR: YOU ARE NOT AN AUTHORIZED USER...GOODBYE");
            System.exit(0);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM