Question
Can you help me to implement a try and catch in case the user types a noninteger? thank you import java.util.Scanner; public class
Can you help me to implement a try and catch in case the user types a noninteger? thank you
import java.util.Scanner;
public class borrar2 {
// Main() method
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("This program will calculate the payroll for 5 to 20 employees");
// first we get the number of employees
System.out.print("How many employees you would like to process? ");
// try {
int empNum = Integer.parseInt(input.nextLine());
if (empNum > 20 || empNum < 5) {
System.out.print("\nEmployees numbers must be in range 5 - 20, try again!!!\n");
/* } // Closes try
catch(NumberFormatException nfe) {
System.out.println("You did not enter a number");
System.out.println("This program will end now");
}
*/
System.exit(0);
}
int[][] payRolls = new
int[empNum][5];
// now for each employee we generate 5 random integers between 0 and 12
for (int i = 0; i < empNum; i++) {
for (int j = 0; j < 5; j++) {
payRolls[i][j] = generateRandom(0, 12);
}
}
/**
* Now we call the payRoll method this method will print the statics about weekly earning
*/
calculatePayRoll(payRolls);
}
public static int generateRandom(int min, int max) {
return (int)Math.floor(Math.random()*(max-min+1)+min);
}
/**
* calculatePayRoll method takes a 2D array that contains the weakly hours worked
* for per employee, this method first calls the another method to print the hours worked per
* day as specified in the question,
* and then this method iterates over hours worked list for each employee
* and calculates earning amount
*/
static void calculatePayRoll(int[][] hoursWorked) {
printStatics(hoursWorked);
// this list stores the earning amount
double[] payRolls = new double[hoursWorked.length];
int overtimeCount = 0;
int weaklyHours = 0;
double earnAmount;
for (int j = 0; j <
hoursWorked.length; j++) {
weaklyHours = 0;
earnAmount = 0;
// first calculate the total hours worked in a weak
for (int i = 0; i < hoursWorked[j].length; i++) {
weaklyHours += hoursWorked[j][i];
}
// then check whether the worked hours are more than 40
if (weaklyHours > 40) {
overtimeCount++;
// calculate earning amount according
earnAmount = (double) (((weaklyHours - 40)*21.75) + 40 * 14.50);
} else {
earnAmount = (double)(weaklyHours * 14.50);
}
payRolls[j] = earnAmount;
}
System.out.printf("\nTotal employees that worked overtime : "+ overtimeCount + " \n\nFollowing are the Employees and there Earned Amount:\n");
for (int x = 0; x < payRolls.length; x++) {
System.out.print("\nEmployee "+(x+1)+" = \t$" +payRolls[x]);
}
System.out.print("\n");
}
// following method prints the per day worked in a tabular format
static void printStatics(int[][] hoursWorked) {
System.out.printf("\nPrinting all employees and hours \n\n%14s %5s %5s %5s %5s %5s\n", "Employee No.","MON", "TUE", "WED", "THU", "FRI");
for (int i = 0; i < hoursWorked.length; i++) {
System.out.printf("\n%14s ", "Employee "+ (i+1));
for (int j = 0; j < 5; j++) {
System.out.printf("%5d ",hoursWorked[i][j]);
}
System.out.print("\n");
}
System.out.print("\n\n");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started