Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

When i make the program run, i get the error empty string. There is data in my input file but i cant seem to get

When i make the program run, i get the error empty string. There is data in my input file but i cant seem to get it to read it. The program is supposed to read data from file EmployeePayroll.txt (Copy pasted below) and write to EmployeePayStubs.txt with the format that the user encounters when inputting data. How do i make it so that i can read the data as a double and continue to write?

Rashid night 60 20 Chin day 30 15 Tran day 45 10 Deeshan night 55 11.5 Serena day 24 10 Deepak day 66 12 Tyrone night 11 18 Andre night 80 15

My Code

import java.util.Scanner; import java.io.*; import java.io.FileWriter; import java.util.NoSuchElementException;

public class SalaryCalcLoop{ double pay=0, OTpay=0; void gpay(double hours, double wage){ if (hours <= 40){ pay = hours * wage; OTpay = 0; } else{ double hr, OThr; hr = 40; OThr = hours - hr; pay = hr * wage; OTpay = OThr *(wage * 1.5); } } public static void main(String[] args) throws IOException {

Scanner input = new Scanner(System.in); String name; String shifts; int shift; Double wage, hours; int sentinel = 1;

while (sentinel != 0){ System.out.println("Please enter employee name"); name = input.next(); System.out.println("Please enter the shift."); System.out.println("For the DAY shift enter 1. For the NIGHT shift enter 2."); shift = input.nextInt(); System.out.println("How many hours did the employee work this week?"); hours = input.nextDouble(); System.out.println("Please enter your hourly wage."); wage = input.nextDouble(); SalaryCalcLoop s = new SalaryCalcLoop(); s.gpay(hours, wage); double totalpay = s.pay + s.OTpay; System.out.println("Employee Name: "+name); System.out.println("Employee's regular pay: "+s.pay); System.out.println("Employee's overtime pay: "+s.OTpay); System.out.println("Employee's total pay: "+totalpay); if(shift == 1){ System.out.println("Employee's pay period is Friday"); } else if(shift == 2){ System.out.println("Employee's pay period is Saturday"); } System.out.println("Do you wish to enter another employee?"); System.out.println("Enter a number to continue or 0 to end"); sentinel = input.nextInt(); } File payroll = new File("EmployeePayroll.txt"); Scanner r = new Scanner(payroll); File output = new File("EmployeePayStubs.txt"); FileWriter w = new FileWriter(output); while (r.hasNextLine()){ String ename = r.nextLine(); String eshift = r.nextLine(); String ehours = r.nextLine(); String ewage = r.nextLine(); writeToFile(ename, eshift, ehours, ewage, w); } r.close(); w.close(); } static void writeToFile(String ename, String eshift, String ehours, String ewage, FileWriter w){ SalaryCalcLoop l = new SalaryCalcLoop(); l.gpay(Double.valueOf(ehours), Double.valueOf(ewage)); double totalpay = l.pay + l.OTpay; try{ w.write("Employee Name: " + ename + ' '); w.write("Employee's regular pay: " + l.pay + ' '); w.write("Employee's overtime pay: " + l.OTpay + ' '); w.write("Employee's total pay: " + totalpay + ' '); if (eshift.equals("day")) { w.write("Employee's pay period is Friday" + " "); } else if (eshift.equals("night")) { w.write("Employee's pay period is Saturday" + " "); } } catch (final Exception e) { System.out.println(e); } } }

*The salary class was SalaryCalcLoop, i had forgotten to change the name

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Explain how work fulfills basic human needs.

Answered: 1 week ago