Question

Java Programming Reading from a Text File

Write a Java program that will use an object of the Scanner class to read employee payroll data from a text file The Text fil

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Payroll
{
public static void main(String[] args)
{
final String fileName="payroll.dat";
File file = new File(fileName);
try
{
// Create a new Scanner object which will read the data
// from the file passed in. To check if there are more
// line to read from it we check by calling the
// scanner.hasNextLine() method. We then read line one
// by one till all lines is read.
Scanner scanner = new Scanner(file);
  
String line = scanner.nextLine();
String []id=line.split("\t");
line = scanner.nextLine();
String []lastName=line.split("\t");
line = scanner.nextLine();
String []firstName=line.split("\t");
line = scanner.nextLine();
String []hoursWorked=line.split("\t");
line = scanner.nextLine();
String []hourlyRate=line.split("\t");
float []regularPay=new float[hourlyRate.length];
float []overtimePay=new float[hourlyRate.length];

for(int i=0;i<hourlyRate.length;i++)
{
regularPay[i]=calculateReaularPay(Float.parseFloat(hoursWorked[i]),Float.parseFloat(hourlyRate[i]));
overtimePay[i]=calculateOvertimePay(Float.parseFloat(hoursWorked[i]),Float.parseFloat(hourlyRate[i]));
}
display(id,lastName,firstName,hourlyRate,hoursWorked,regularPay,overtimePay);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}

private static float calculateReaularPay(float hrWorked, float hrRate) {
if(hrWorked<=40)
return hrRate*hrWorked;
else
return hrRate*40;
}

private static float calculateOvertimePay(float hrWorked, float hrRate)
{
if(hrWorked>40)
return (float)((hrWorked-40)*(1.5*hrRate));
else
return 0;
}

private static void display(String[] id, String[] lastName, String[] firstName, String[] hourlyRate, String[] hoursWorked, float[] regularPay, float[] overtimePay) {
System.out.printf("%-15s%-15s%-15s%-15s%-15s%-15s%-15s%-15s\n","Emp Id","Last Naame","First Name","Hours Worked","Rate Hourly","Regular pay","Overtime Pay","Total pay");
for(int i=0;i<id.length;i++)
{
System.out.printf("%-15s%-15s%-15s%-15.2f%-15.2f%-15.2f%-15.2f%-15.2f\n",id[i],lastName[i],firstName[i],Float.parseFloat(hoursWorked[i]),Float.parseFloat(hourlyRate[i]),regularPay[i],overtimePay[i],(regularPay[i]+overtimePay[i]));
}
}
  
}
output

Payroll from file - NetBeans IDE 8.0.1 Search (Ctrl+1) File Edit View Navigate Source Refactor Ru Debug Profile Team Iools Wi

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Java Programming Reading from a Text File Write a Java program that will use an object...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java program to read customer details from an input text file corresponding to problem...

    Write a Java program to read customer details from an input text file corresponding to problem under PA07/Q1, Movie Ticketing System The input text file i.e. customers.txt has customer details in the following format: A sample data line is provided below. “John Doe”, 1234, “Movie 1”, 12.99, 1.99, 2.15, 13.99 Customer Name Member ID Movie Name Ticket Cost Total Discount Sales Tax Net Movie Ticket Cost Note: if a customer is non-member, then the corresponding memberID field is empty in...

  • JAVA Code: Complete the program that reads from a text file and counts the occurrence of...

    JAVA Code: Complete the program that reads from a text file and counts the occurrence of each letter of the English alphabet. The given code already opens a specified text file and reads in the text one line at a time to a temporary String. Your task is to go through that String and count the occurrence of the letters and then print out the final tally of each letter (i.e., how many 'a's?, how many 'b's?, etc.) You can...

  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • (Java) Rewrite the following exercise below to read inputs from a file and write the output...

    (Java) Rewrite the following exercise below to read inputs from a file and write the output of your program in a text file. Ask the user to enter the input filename. Use try-catch when reading the file. Ask the user to enter a text file name to write the output in it. You may use the try-with-resources syntax. An example to get an idea but you need to have your own design: try ( // Create input files Scanner input...

  • Description: This Java program will read in data from a file students.txt that keeps records of...

    Description: This Java program will read in data from a file students.txt that keeps records of some students. Each line in students.txt contains information about one student, including his/her firstname, lastname, gender, major, enrollmentyear and whether he/she is a full-time or part-time student in the following format. FIRSTNAME      LASTNAME        GENDER              MAJORENROLLMENTYEAR FULL/PART The above six fields are separated by a tab key. A user can input a keyword indicating what group of students he is searching for. For example, if...

  • I need this java program to contain methods that do some of the work that can...

    I need this java program to contain methods that do some of the work that can be modularized. It must logically be the same as this loop program but instead the logic will be modularized into Java Methods. Continue to take input for every employee in a company, and display their information until a sentinel value is entered, that exits the loop and ends the program. import java.util.Scanner; public class SalaryCalc { double Rpay = 0, Opay = 0; void...

  • I need to write a program in java that reads a text file with a list...

    I need to write a program in java that reads a text file with a list of numbers and sorts them from least to greatest. This is the starter file. import java.util.*; import java.io.*; public class Lab3 { static final int INITIAL_CAPACITY = 5; public static void main( String args[] ) throws Exception { // ALWAYS TEST FOR REQUIRED INPUT FILE NAME ON THE COMMAND LINE if (args.length < 1 ) { System.out.println("\nusage: C:\\> java Lab3 L3input.txt\n"); System.exit(0); } //...

  • You will write a single java program called MadLibs. java.

    You will write a single java program called MadLibs. java. This file will hold and allow access to the values needed to handle the details for a "MadLibs" game. This class will not contain a maino method. It will not ask the user for any input, nor will it display (via System.out.print/In()) information to the user. The job of this class is to manage information, not to interact with the user.I am providing a MadLibsDriver.java e^{*} program that you can...

  • Create a Text file containing the following data (without the headings): Name            Wage      Hours Callaway, G 16.00       40 Hanson, P     15.00      48 Lasard, D      16.50      35 Stillman,...

    Create a Text file containing the following data (without the headings): Name            Wage      Hours Callaway, G 16.00       40 Hanson, P     15.00      48 Lasard, D      16.50      35 Stillman, W   18.00      50 Write a C++ program that uses the information in the file to produce the following pay report for each employee: Name, Pay Rate, Hours, Regular Pay, Overtime Pay, Gross Pay       Compute regular pay as  any hours worked up to and including 40 hours multiplied by the pay rate. Compute overtime...

  • Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions...

    Q22 Consider the following 2 text files and Java program. 8 Points Answer the following questions keeping in mind that: • All three files are in the same directory, and there are no other files. • The program may crash. If the program crashes, answer the question with: "The program crashes" standings1.txt: 1 Valtteri Bottas 25 2 Charles Leclerc 18 3 Lando Norris 16 4 Lewis Hamilton 12 standings2.txt: 1 Valtteri Bottas 43 2 Lewis Hamilton 37 3 Lando Norris...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT