Java:异常帮助Char和Int

时间:2014-08-21 21:15:35

标签: java exception-handling try-catch throws

好的,我已经再次使用此代码了。

我需要输入一个不允许用户输入0的异常(因为你以后不能将0除以)并且用户不能输入字母字符。我试图显示消息,忽略错误的输入,并循环以允许用户再次尝试,直到他们输入可接受的数字。

这就是我所拥有的:

package exceptionhandler;

/**
 *
 * @author Sarah
 */
import java.util.Scanner;

public class ExceptionHandler {

    /**
     * @param args
     *            the command line arguments10 10
     */
    public static void main(String[] args) throws NumberFormatException {
        Scanner in = new Scanner(System.in);
        Scanner input = new Scanner(System.in);


        System.out.print("Please enter ten values:");
        System.out.println();
    // Input the data into array from the user.
        double[ ] digit = new double[11];
        int sum = 0;
        //Declare an array
        try
        {
        for (int i = 1; i < digit.length; i++) {
            System.out.print("Value " + i + ": ");
            digit[i] = (double)in.nextInt();
            sum += (int)digit[i];
        }
        catch (NumberFormatException e)
        {
            System.out.println("You Can Only Enter Numbers!");
         }
        }

        System.out.println("Total Values in Array:"+ sum);
         // Calculate the sum and print the total


        System.out.println();
        System.out.println("Would you like to divide the values?");
        System.out.println("Yes or No to Exit the Program");
        String a = input.next();


         if(a.equalsIgnoreCase("yes")){   
                double [] divisionResult = new double[digit.length / 2];
    //Division array declared
                for (int i = 1; i < digit.length; i += 2)
                {
                double result = digit[i];
                if (result > digit[i + 1]) 
                result = result / digit[i + 1]; 
        else {
                result = digit[i + 1] / result;
            }
                divisionResult [i / 2] = result;
                    System.out.println(result);
                }
            }
        else if(a.equalsIgnoreCase("no")){
           System.exit(0);
        }
        }
}

我已经尝试声明抛出异常然后尝试了try..catch。但它并没有认识到捕获并试图互相交谈......所以我知道我做错了什么,但我不知道应该去哪里。

异常是否在正确的位置?我应该做别的吗?我的异常写错了吗?那么我怎样才能继续防止输入零和重新抛出?

帮助?

1 个答案:

答案 0 :(得分:0)

在输入字符时应该InputMismatchException而不是NumberFormatException来捕获异常,如果用户输入0和deduct 1,则需要检查 0 for-loop的当前索引让用户再试一次。

<强>样品:

for (int i = 1; i < digit.length; i++) {
        try {
            System.out.print("Value " + i + ": ");
            digit[i] = (double) in.nextInt();
            sum += (int) digit[i];
            if(digit[i] == 0.0)
            {
                System.out.println("You cant enter 0: try again");
                --i;
            }
        } catch (InputMismatchException e) {
            System.out.println("You Can Only Enter Numbers!");
            --i;
            in.nextLine(); //to consume the character
        }
    }

<强>结果:

Please enter ten values:
Value 1: 0
You cant enter 0: try again
Value 1: asd
You Can Only Enter Numbers!
Value 1: