Java中没有用户输入的异常处理 [英] Exception Handling for no user input in Java

查看:221
本文介绍了Java中没有用户输入的异常处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使程序进入异常处理,如果用户未输入任何内容,则他们将收到错误消息错误,输入大于0的美元金额"或错误,输入1、2或3" .截至目前,如果用户只是在没有输入的情况下按输入",则该程序将不执行任何操作.

I am trying to get my program to exception handle for if the user inputs nothing so they will get an error message of "Error, enter a dollar amount greater than 0" or "Error, Enter a 1, 2 or 3". As of now, the program does nothing if the user just hits "enter" with no input....

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));
}

}

推荐答案

您可以编写method来验证输入,然后再继续.如果用户输入的内容无效,它可以继续要求输入.例如.下面的示例演示如何验证integer输入:

You can write a method that validates the input before proceeding. It can keep asking for inputs if user enters something that is not valid. E.g. below example demonstrates how to validate an integer input:

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;
}

这篇关于Java中没有用户输入的异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆