Exception Handling for no user input in Java

别说谁变了你拦得住时间么 提交于 2019-12-12 00:48:48

问题


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

}

回答1:


Replace input.next();

with input.nextLine();




回答2:


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


来源:https://stackoverflow.com/questions/41050343/exception-handling-for-no-user-input-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!