Java中没有用户输入的异常处理

时间:2016-12-08 23:01:49

标签: java inputmismatchexception

我正在尝试将我的程序设置为异常句柄,如果用户没有输入任何内容,那么他​​们将收到错误消息"错误,输入大于0的金额"或"错误,输入1,2或3"。截至目前,如果用户只是点击"输入"没有输入....

import java.util.Scanner;
import java.util.*;
import java.text.DecimalFormat;

public class Candleline
{
    public static void main(String[] args)
    {
        //initiate scanner
        Scanner input = new Scanner(System.in);

        System.out.println("\tCandleLine - Candles Online");
        System.out.println(" ");

        //declare variables and call methods
        double candleCost = getCandleCost();
        int shippingType = getShippingType();
        double shippingCost = getShippingCost(candleCost, shippingType);


        output(candleCost, shippingCost);

    }

public static double getCandleCost()
    {
        //get candle cost and error check
        Scanner input = new Scanner(System.in);
        boolean done = false;
        String inputCost;
        double candleCost = 0;
        while(!done)
            {
                System.out.print("Enter the cost of the candle order: ");

                try
                {

                    inputCost = input.next();
                    candleCost = Double.parseDouble(inputCost);
                    if (inputCost == null) throw new InputMismatchException();
                    if (candleCost <=0) throw new NumberFormatException();
                    done = true;
                }
                catch(InputMismatchException e)
                {
                    System.out.println("Error, enter a dollar amount greater than 0");
                    input.nextLine();
                }
                catch(NumberFormatException nfe)
                {
                    System.out.println("Error, enter a dollar amount greater than 0");
                    input.nextLine();
                }

            }
        return candleCost;
    }


    public static int getShippingType()
        {
            //get shipping type and error check
            Scanner input = new Scanner(System.in);
            boolean done = false;
            String inputCost;
            int shippingCost = 0;
            while(!done)
                {
                    System.out.println(" ");
                    System.out.print("Enter the type of shipping: \n\t1) Priority(Overnight) \n\t2) Express (2 business days) \n\t3) Standard (3 to 7 business days) \nEnter type number: ");


                    try
                    {
                        inputCost = input.next();
                        shippingCost = Integer.parseInt(inputCost);
                        if (inputCost == null) throw new InputMismatchException();
                        if (shippingCost <=0 || shippingCost >= 4) throw new NumberFormatException();
                        done = true;
                    }
                    catch(InputMismatchException e)
                    {
                        System.out.println("Error, enter a 1, 2 or 3");
                        input.nextLine();
                    }
                    catch(NumberFormatException nfe)
                    {
                        System.out.println(" ");
                        System.out.println("Error, enter a 1, 2 or 3");
                        input.nextLine();
                    }

                }
            return shippingCost;
    }

    public static double getShippingCost(double candleCost, int shippingType)
    {
        //calculate shipping costs
        double shippingCost = 0;


        if (shippingType == 1)
        {
            shippingCost = 16.95;
        }
        if (shippingType == 2)
        {
            shippingCost = 13.95;
        }
        if (shippingType == 3)
        {
            shippingCost = 7.95;
        }
        if (candleCost >= 100 && shippingType == 3)
        {
            shippingCost = 0;
        }
        return shippingCost;
}

public static void output(double fCandleCost, double fShippingCost)
{
        //display the candle cost, shipping cost, and total
        Scanner input = new Scanner(System.in);
        DecimalFormat currency = new DecimalFormat("$#,###.00");
        System.out.println("");
        System.out.println("The candle cost of " + currency.format(fCandleCost) + " plus the shipping cost of " + currency.format(fShippingCost) + " equals " + currency.format(fCandleCost+fShippingCost));
}

}

2 个答案:

答案 0 :(得分:0)

替换input.next();

使用input.nextLine();

答案 1 :(得分:0)

您可以编写一个method来验证输入,然后再继续。如果用户输入无效的内容,它可以继续询问输入。例如。下面的示例演示了如何验证integer输入:

private static int getInput(){
    System.out.print("Enter amount :");
    Scanner scanner = new Scanner(System.in);
    int amount;
    while(true){
        if(scanner.hasNextInt()){
            amount = scanner.nextInt();
            break; 
        }else{
            System.out.println("Invalid amount, enter again.");
            scanner.next();
        }
    }
    scanner.close();
    return amount;
}