In: Computer Science
4 Run-time error, program crashed during execution. Not sure why you are using an array[] to get input, not necessary and it seems to imply a comma separated file. The input file I provided has each data element on its own line. But it is the array[] , splittedLine, that is causing your run-time error.
code
import java.util.Scanner;
public class SalaryCalcModularized{
public static void compute(double hrlyPayRate,double hrs,String
name,String shift){
// Calculate regular and overtimepay
double regularPay = Math.min(40, hrs) * hrlyPayRate;
double overtimePay = Math.max(0, hrs - 40) * 1.5 *
hrlyPayRate;
System.out.println("Employee " + name);
System.out.println("Regular Pay: $" + regularPay);
System.out.println("Overtime Pay: $" + overtimePay);
System.out.println("Total Gross Pay: $" + (regularPay +
overtimePay));
if (shift.equals("day")) {
System.out.println("Friday pay period");
} else {
System.out.println("Saturday pay period");
}
}
public static void read_input(){
// Scanner to read user input
Scanner obj = new Scanner(System.in);
// Prompt employee name
System.out.print("Enter Employee Name: ");
String name = obj.nextLine();
// Prompt shift
System.out.print("Enter Employee Shift: ");
String shift = obj.nextLine();
// Prompt hours worked
System.out.print("Enter hours worked: ");
double hrs = Double.parseDouble(obj.nextLine());
// Prompt payrate
System.out.print("Enter hourly pay rate: ");
double hrlyPayRate = Double.parseDouble(obj.nextLine());
compute( hrlyPayRate,hrs, name, shift);
}
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
String ch = "y";
while (ch.equalsIgnoreCase("y")) {
read_input();
System.out.println("Do you want continue? [Y/N]");
ch = ob.next();
ob.nextLine();
}
}
}
import java.util.Scanner;
public class SalaryCalcModularized{
public static void compute(double hrlyPayRate,double hrs,String
name,String shift){
// Calculate regular and overtimepay
double regularPay = Math.min(40, hrs) * hrlyPayRate;
double overtimePay = Math.max(0, hrs - 40) * 1.5 *
hrlyPayRate;
System.out.println("Employee " + name);
System.out.println("Regular Pay: $" + regularPay);
System.out.println("Overtime Pay: $" + overtimePay);
System.out.println("Total Gross Pay: $" + (regularPay +
overtimePay));
if (shift.equals("day")) {
System.out.println("Friday pay period");
} else {
System.out.println("Saturday pay period");
}
}
public static void read_input(){
// Scanner to read user input
String name=""; String shift=""; double hrs =0.0; double
hrlyPayRate =0.0;
Scanner obj = new Scanner(System.in);
// Prompt employee name
System.out.print("Enter Employee Name: ");
if (obj.hasNextLine()) {
name= obj.nextLine();
}
else
return;
// Prompt shift
System.out.print("Enter Employee Shift: ");
if (obj.hasNextLine()) {
shift= obj.nextLine();
}
else
return;
System.out.print("Enter hours worked: ");
if (obj.hasNextLine()) {
hrs = Double.parseDouble(obj.nextLine());
}
else
return;
//double hrs = Double.parseDouble(obj.nextLine());
// Prompt payrate
System.out.print("Enter hourly pay rate: ");
if (obj.hasNextLine()) {
hrlyPayRate = Double.parseDouble(obj.nextLine());
}
else
return;
compute( hrlyPayRate,hrs, name, shift);
//obj.close();
}
public static void main(String[] args) {
Scanner ob=new Scanner(System.in);
String ch = "y";
while (ch.equalsIgnoreCase("y")) {
read_input();
System.out.println("Do you want continue? [Y/N]");
if (ob.hasNextLine()) {
ch = ob.nextLine();
}
else
return;
ob.nextLine();
}
}
}