简体   繁体   中英

How can I error handle my code to prevent my program from crashing when the user inputs a decimal point?

this is in java, my problem is that when the user inputs a "depositAmount" the program crashes if they use a decimal point or comma, i understand that their input needs to be of type double, but i do not know how to say "if the user's input is a double, accept it, else, ask for a new input"

import java.util.Scanner;
import java.util.ArrayList;
public class Engine

{

private static ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();

public static void Engine()
{

    Scanner Reader = new Scanner(System.in);
    BankAccount n = new BankAccount();
    boolean keepGoing = true;

    while(keepGoing)
    {
        System.out.println("Welcome to The Bank of Money, what would you like to do?\n enter n to create a new account, enter e to use an existing account, or enter q to quit to main menu");
        String response = Reader.nextLine();
        if(response.equals("q")) keepGoing = false;
        else if(response.equals("n")) 
        {
            accounts.add(new BankAccount());
            System.out.println("Your account number is: " + accounts.get(accounts.size()-1).getAccountNum());
        }
        else if(response.equals("e"))
        {
            System.out.println("what is your account number?");
            Scanner Reader1 = new Scanner (System.in);
            int accountNum = Reader1.nextInt();  
            BankAccount selectedAccount = null;
            for(int i = 0; i<accounts.size();i++)
            {
                if (accounts.get(i).getAccountNum()==accountNum)
                {
                    selectedAccount = accounts.get(i);

                }
            }
            if(selectedAccount ==null)System.out.println("There is no such account. Please try again.");
            else transact(selectedAccount);
        }
    }
}

public static void transact(BankAccount selectedAccount)
{
    Scanner in = new Scanner(System.in);
    int response = 0;
    while(response!=7)
    {
        BankAccount.printTransactionMenu();
        response = in.nextInt();
        if(response==1)
        {
            System.out.println("Enter the amount that you would like to deposit");
            System.out.print("$");
            Scanner depositScanner = new Scanner(System.in);
            double depositAmount = depositScanner.nextInt();
        }
        if(response==2)
        {
            System.out.println("Enter the amount that you would like to withdraw");
            System.out.print("$");
            Scanner withdrawScanner = new Scanner(System.in);
            double withdrawAmount = withdrawScanner.nextInt();
            if(withdrawAmount>selectedAccount.getBalance()) 
                System.out.println("Insufficient funds");
            else selectedAccount.withdraw(withdrawAmount);
        }
        if(response==3)
        {
            selectedAccount.assessInterest();
            System.out.println("Your new balance is $" + selectedAccount.getBalance());
        }
        if(response==4)
        {
            System.out.println("Your account balance is $" + selectedAccount.getBalance());
        }
        if(response==5)
        {
            System.out.println("Which account would you like to transact with?");
            Scanner Reader1 = new Scanner (System.in);
            int transactAccountNum = Reader1.nextInt();  
            BankAccount transactAccount = null;
            for(int i = 0; i<accounts.size();i++)
            {
                if (accounts.get(i).getAccountNum()==transactAccountNum)
                {
                    transactAccount = accounts.get(i);
                }
            }
            if(transactAccount ==null)System.out.println("There is no such account. Please try again.");
            else 
            {   
                System.out.println("What amount would you like to transfer?");

                Scanner transferScanner = new Scanner(System.in);
                double transactAmount = transferScanner.nextDouble();
                selectedAccount.transfer(transactAccount, transactAmount);
            }
        }
        if(response==6)
        {
            selectedAccount.printStatement();
        }
    }
}

}

You can take in an input from Scanner that does not initially have an int or double value, using the next() method. If you know the data type, you can check to see if it has a next double (hasNextDouble) or whatever data type you require. If not, you can take in a String type and convert it.

Scanner in = new Scanner(System.in);
String s = in.next();

From there you can parse it into the appropriate data type (eg double).

double d = Double.parseDouble(s);

I hope this helped!

You can easily use try/catch segment in java for this purpose. In case that the user has entered an integer instead of decimal the program will rise a Cast Exception and then you can handle it as you wish (like asking user to enter again).

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