Exception Handling for no user input in Java

2019-09-11 03:35发布

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

}

2条回答
霸刀☆藐视天下
2楼-- · 2019-09-11 04:03

Replace input.next();

with input.nextLine();

查看更多
男人必须洒脱
3楼-- · 2019-09-11 04:04

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;
}
查看更多
登录 后发表回答