InputMismatchexception不起作用

时间:2011-06-22 11:49:20

标签: java

我创建了一个程序来检查整数变量“paycategory”的输入范围,但是当我想检查inputmismatch错误时。我尝试了但它不起作用...我不想将数据类型更改为字符串,因为paycategory应该是整数。请帮忙解决这个问题?

import java.util.Scanner;
import java.util.*;
import java.text.*;
import java.lang.Object.*;

public class TestEmployeePayRoll {

     public static void main(String Args[])
     {
        String EmployeeID = null, FirstName = null, LastName = null, result;
        double HoursWorked;
        int PayCategory = 0, counter = 0;

      do
      {  
        Scanner input = new Scanner(System.in);

        try 
            {
            int flag = 1;
            String input1 ;
            System.out.println("Enter your Employee ID number: ");
            while(flag==1){

            EmployeeID = input.nextLine();

            if(EmployeeID.trim().length()>=5){
                    flag = 0;

                    System.out.println("Enter the First Name: ");
                    FirstName = input.nextLine();

                    System.out.println("Enter Last Name: "+ " ");
                    LastName = input.nextLine();

            }else
            System.out.print("EmployeeID must be exactly 5 digits long: \n Enter the Employee ID again: ");
            }

            }
            catch(Exception e)
            {
               System.out.println("Exception ");
            }

         try{


        do{
            System.out.println("Enter the Pay Category: "+ " ");
            PayCategory = input.nextInt();

        if(!(PayCategory >0 || PayCategory <5))throw new InputMismatchException();
        {
            System.out.println("Pay Category must be between 1 and 4");
        }
        }

        while(PayCategory < 1 || PayCategory > 4);
         }

         catch(InputMismatchException e)
         {
             System.out.println("PayCategory must be integers");

         } 
        do
        {
            System.out.println("Enter the number of hours worked: ");
            HoursWorked = input.nextDouble();
            Double hours = new Double(HoursWorked);
            if(hours.isNaN())
        {
            System.out.println("---Enter a valid hours value---");
        }

            else if(!(HoursWorked >1 || HoursWorked <80))
        {
            System.out.println("---Enter value between 1 and 80---");
        }
        }

        while(HoursWorked < 1 || HoursWorked > 80);

        EmployeePayRoll obj1 = new EmployeePayRoll(FirstName, LastName, EmployeeID, HoursWorked, PayCategory);

        DecimalFormat fmt = new DecimalFormat("###,##0.00");

        System.out.println("\n-----------------------------------------------------");
        System.out.println("\n The pay details for:" + obj1.getName() + "\t\t\t" + "ID:" + EmployeeID);
        System.out.println("\n-----------------------------------------------------");
        System.out.println("Pay Category: \t\t\t" + obj1.getPayCategory());
        System.out.println("Hours Worked: \t\t\t" + obj1.getHoursWorked());
        System.out.println("Pay Rate: \t\t\t" + obj1.getPayRate());
        System.out.println("Gross Pay:  \t\t\t" + "$"+fmt.format(obj1.getGrossPay()));
        System.out.println("Tax Payable:  \t\t\t" + "$"+fmt.format(obj1.getTaxPayable()));
        System.out.println("\t\t\t\t---------");
        System.out.println("Net Pay: \t\t\t" + "$" + fmt.format(obj1.getNetPay()));
        System.out.println("\n------------------------------------------------------");
        System.out.println();
        System.out.println("\n Process another employee? (Y/N)");
        result = input.next();

        }
            while (result.equals("Y")||result.equals("y"));

    }

 }

由于

3 个答案:

答案 0 :(得分:1)

以下条件永远不会成立,因此永远不会抛出InputMismatchException

if(!(PayCategory >0 || PayCategory <5))
    throw new InputMismatchException();

纠正你的病情。

答案 1 :(得分:1)

阅读De Morgan's laws

!(PayCategory >0 || PayCategory <5)相当于!(PayCategory>0) && !(PayCategory<5)

答案 2 :(得分:0)

Dogbane的答案非常正确,但这个问题在你的代码中很常见:你的逻辑条件过于复杂,这使得错误难以发现。

采取行:

  else if (!(HoursWorked > 1 || HoursWorked < 80))

小时工作将始终是> 1或者&lt;因此,括号内的条件将始终为真,反之则永远不会。尝试使您的逻辑条件更简单,以便您可以轻松地查看它们是什么而不是使用!操作员,如果没有必要的话。

这个条件应该是:

  else if (HoursWorked < 1 || HoursWorked > 80)