开发者

how to check the int range in java and limit it only 5digits

Please help with my assignment. Here is the question:

Create a separate test driver class called TestEmployeePayroll that will test the EmployeePayroll class by performing开发者_高级运维 the following:

Prompt the user to enter the employees’ ID number, First name, Last name, Pay Category and Hours worked (one at a time).

  • The user entry for employees ID number must be exactly 5 digits long.
  • The user entry for Category must only be accepted if it is in the range 1 to 4.
  • The user entry for Hours worked must only be accepted if it is the range 1 to 80.

This is what I did till now:

import java.util.Scanner;
public class TestEmployeePayRoll {

    public static void main(String[] args){
        EmployeePayRoll obj1 = new EmployeePayRoll();

        Scanner input = new Scanner(System.in);
        System.out.println("Enter the Employee ID number: "+ " ");
        String EmployeeID = input.nextLine();

        //How to check the range here if int is 5 digits long or not ?

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

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

        System.out.println("Enter the Pay Category: "+ " ");
        double PayCategory = input.nextDouble();

        System.out.println("Enter the number of hours worked: "+ " ");
        double HoursWorked = input.nextDouble();
    }
}


You will probably want to use Integer.parseInt().


You can count the length of a String and then convert it to number, Oli Charlesworth told you how to convert it, or you can measure the number. It depends on what you want. Is 012345 a valid ID? It's a 6 char String but it is less than the biggest 5 digits number.


I think you almost got it...

import java.util.Scanner;
public class TestEmployeePayRoll {

    public static void main(String[] args){
        // ... get the values, as you are doing already
        // validate input
        int employeeIdAsInteger = validateAndConvertEmployeeId(EmployeeId);
        int payCategoryAsInteger = validateAndConvertPayCategory(PayCategory);
        // ... and so on
    }

    private int validateAndConvertEmployeeId(String employeeId) {
        // The user entry for employees ID number must be exactly 5 digits long.
        if (employeeId == null || employeeId.trim().length() != 5) {
            throw new IllegalArgumentException("employee id must be exactly 5 digits long");
        }
        // will throw an exception if not a number...
        return Integer.parseInt(employeeId);
    }
    // ...
}


Depending on your objectives & constraints, you could look into the Pattern class and use a regular expression.


You can check for conditions like this.

import java.util.Scanner;

public class TestEmployeePayRoll {

    public static void main(String[] args) {
        TestEmployeePayRoll obj1 = new TestEmployeePayRoll();

        Scanner input = new Scanner(System.in);
        System.out.println("Enter the Employee ID number: " + " ");
        String EmployeeID = input.nextLine();

        if (EmployeeID.trim().length() != 5) {
            System.out.println("--- Enter valid Employee ID number ---");
        }
        System.out.println("Enter the first Name: " + " ");
        String FirstName = input.nextLine();

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

        System.out.println("Enter the Pay Category: " + " ");

        double PayCategory = input.nextDouble();

        Double pay = new Double(PayCategory);

        if (pay.isNaN()) {
            System.out.println("***** Enter a valid Pay Category *****");
        }
        if (!(PayCategory >= 0 && PayCategory <= 5)) {
            System.out.println(" --- PayCategory must be between 0 and 5");
        }

        System.out.println("Enter the number of hours worked: " + " ");

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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜