Input is from a file, and output is written to a file. Input: The input file has
ID: 3849194 • Letter: I
Question
Input is from a file, and output is written to a file. Input: The input file has the needed data on a single line. There is text as well as the numerical inputs. One sample input line is Pay rate: exist15.50 per hour. Weekly hours worked: 25 17.5 30 35.75 32 A second example of an input line is Pay rate: exist12 Hours worked each week: 40 38 30.5 27.5 39 So, the text can vary, but you can count on (1) a dollar sign (exist) appears just before the value of pay per hour, (2) a colon (:) appears just before the five values for the number of hours worked each week, and (3) the hours worked values are separated by spaces. Your program must be able to handle ANY input line that meets these specifications. (You cannot have a different version of the program for each input line.) Output: The output file will have a table with 2 columns. The left column has the labels telling what each value is: labels are left-justified. The right column has the money value, with 2 decimal places. There is a dollar sign (exist) to the left: the money amounts line up on the right (right-justified) so all the decimal points line up vertically. A sample output follows. (The values are for exist15 per hour, and 40 hours worked each week.)Explanation / Answer
The question needs more details on exercise 18. Also if you can provide a sample input file for the program to work on, the output can be matched.
I have give you the code to process the line format containing payrate and hours worked as described below. Code and output shown below. Please post a comment if any further help is needed. I shall respond to your comment.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadInput {
private static void readFile(String filename)
{
try {
Scanner inputFile = new Scanner(new File(filename));
String line = inputFile.nextLine();
double payrate;
double[] hours;
//first extract the pay rate following the $ sign
int idx1=line.indexOf('$');
int idx2 = line.indexOf(' ', idx1+1);
String paystr =line.substring(idx1+1, idx2);
payrate = Double.parseDouble(paystr);
//now extract the hours starting after :
idx1 = line.indexOf(':',idx2);
String hoursWorked = line.substring(idx1+1).trim();
String tokens[] = hoursWorked.split(" "); //split based on spaces between
hours = new double[tokens.length];
for(int i = 0; i < hours.length; i++)
{
hours[i] = Double.parseDouble(tokens[i]);
}
double total = 0;
System.out.println("Pay rate is $" + payrate);
System.out.println("Hours worked is ");
for(int i = 0; i< hours.length; i++)
{
total += hours[i];
System.out.println(hours[i]);
}
System.out.println("Total hours is " + total);
System.out.printf("Total pay is %.2f" , total * payrate); //show with 2 decimal places
} catch (FileNotFoundException e) {
System.out.println(e.getMessage());
}
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.print(" Enter input file name : ");
String filename = keyboard.nextLine().trim();
readFile(filename);
}
}
input file payrate.txt
Pay rate: $15.50 per hour. Weekly hours worked: 25 17.5 30 35.75 32
output
Enter input file name : payrate.txt
Pay rate is $15.5
Hours worked is
25.0
17.5
30.0
35.75
32.0
Total hours is 140.25
Total pay is 2173.88