简体   繁体   English

在 try/catch 中进行异常处理的扫描器输入未按预期工作

[英]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.我正在制作一个接受帐号和密码的 atm 程序。 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.当我尝试输入除接受的帐户和预先确定的 pin 之外的任何内容时,它应该会给出一条错误消息,然后用户再试一次。 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. whiles 只检查帐号是否无效。 When a parsing error occurs the account / pin variable is still 0 so it doesn't repeat.当发生解析错误时, account / pin变量仍为0因此不会重复。

I fixed/improved the code for you.我为您修复/改进了代码。 Note you should always debug your code before you ask a question on StackOverflow.请注意,在您在 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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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