Results 1 to 5 of 5
- 03-22-2011, 02:27 AM #1
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
Extracting values from a text file
Good day!
I have a program here that gets a record from a text file (employee.txt) and creates a daily time record (dtr) for a selected record.
Here's my code:
And produces the following output (ex. if 1 is chosen )Java Code:import java.io.*; public class JavaPayroll { public static void main (String[] args) throws IOException { DataInputStream k = new DataInputStream(System.in); FileWriter fw = new FileWriter("dtr.txt"); BufferedWriter bw = new BufferedWriter(fw); FileReader fr = new FileReader("employee.txt"); BufferedReader br = new BufferedReader(fr); String [] days = {"Mon","Tue","Wed","Thu","Fri"}; String timein; String timeout; int count = 5; String rey[]=new String[count]; for(int r=0; r<count; r++) { rey[r]=br.readLine(); System.out.println(rey[r]); } System.out.print("Enter Your Choice : "); String a=k.readLine(); System.out.println(); for(int r=0; r<rey.length; r++) { String j[]=null; j=rey[r].split(" "); if(a.equals(j[0])) { System.out.println("Student No. : "+j[0]); System.out.println("Last Name : "+j[1]); System.out.println("First Name : "+j[2]); } } System.out.println(); for (int counter = 0; counter < days.length; counter++ ){ System.out.print(days[counter]); System.out.print("\tTime in : "); timein = k.readLine(); System.out.print("\tTime out : "); timeout = k.readLine(); bw.write (a+" "+timein+" "+timeout+" "+days[counter]); bw.newLine(); } bw.close(); } }
1 8:00 4:30 Mon
1 3:12 3:45 Tue
1 9:11 12:33 Wed
1 1:00 4:00 Thu
1 5:00 7:30 Fri
Question, how can i extract the time in and time out per day so that i could do some math in computing a per day to to weekly income of an employee?
I hope you guys could lend me a hand in this matter.. Thank you very much !!!
- 03-22-2011, 02:32 AM #2
You can use the SimpleDateFormat and Date classes
- 03-22-2011, 03:48 AM #3
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
I dont know how to use SimpleDateFormat.... Would please give me a sample snippet for it?
Would it be possible that in the first line "1 8:00 4:30 Mon" i could extract the string 8:00 and 4:30 and convert it to int? Thanks!
- 03-22-2011, 03:56 AM #4
Whenever someone mentions a class you have never used before, your first port of call should be the Java API. Then google for a tutorial. In the case of SimpleDateFormat it has a link to a tutorial. Then you read extensively about the class, try and write some code (a test program not your actual assignment). When you have problems come back here, post your code, any exceptions and ask a specific question.
Sure. you can use String.split to get the 8:00 and the 4:30. Then you would split them again to get the hours and minutes. Or you can use Date and SimpleDateFormat (after the first split that is).Would it be possible that in the first line "1 8:00 4:30 Mon" i could extract the string 8:00 and 4:30 and convert it to int? Thanks!
- 03-22-2011, 06:13 AM #5
Member
- Join Date
- Dec 2010
- Posts
- 18
- Rep Power
- 0
So far, i have come up with using a split function to extract the values of every string entry... The problem is that how will i come up with deduction for late if mins arrived is more than 15 mins.
Java Code:import java.io.*; public class JavaPayroll { public static void main (String[] args) throws IOException { DataInputStream k = new DataInputStream(System.in); FileWriter fw = new FileWriter("dtr.txt"); BufferedWriter bw = new BufferedWriter(fw); FileReader fr = new FileReader("employee.txt"); BufferedReader br = new BufferedReader(fr); String [] days = {"Mon","Tue","Wed","Thu","Fri"}; String timein; String timeout; int count = 5; int totalhrs = 0; int totalmins = 0; int perday = 0; String rey[]=new String[count]; for(int r=0; r<count; r++) { rey[r]=br.readLine(); System.out.println(rey[r]); } System.out.print("Enter Your Choice : "); String a=k.readLine(); System.out.println(); for(int r=0; r<rey.length; r++) { String j[]=null; j=rey[r].split(" "); if(a.equals(j[0])) { System.out.println("Student No. : "+j[0]); System.out.println("Last Name : "+j[1]); System.out.println("First Name : "+j[2]); } } System.out.println(); for (int counter = 0; counter < days.length; counter++ ){ System.out.print(days[counter]); System.out.print("\tTime in : "); timein = k.readLine(); System.out.print("\tTime out : "); timeout = k.readLine(); bw.write (a+" "+timein+" "+timeout+" "+days[counter]); String[] myStringArrayin= timein.split(":"); String[] myStringArrayout= timeout.split(":"); int[] myTimein = new int[myStringArrayin.length]; int[] myTimeout = new int[myStringArrayin.length]; for (int i = 0; i < myStringArrayin.length; i++) { myTimein[i] = Integer.parseInt(myStringArrayin[i]); } System.out.println(); for (int i = 0; i < myStringArrayout.length; i++) { myTimeout[i] = Integer.parseInt(myStringArrayout[i]); } int [] myTimeDiff = new int [2]; myTimeDiff[0]=myTimeout[0] - myTimein[0]; myTimeDiff[1]=myTimeout[1] - myTimein[1]; totalhrs = totalhrs + myTimeDiff[0]; totalmins = totalmins + myTimeDiff[1]; bw.newLine(); } if (totalmins > 59){ totalhrs = totalhrs + (totalmins/60); totalmins = totalmins % 60;} //System.out.println(totalhrs + " " + totalmins); perday = totalhrs * 62; System.out.println ("Salary is " + perday); bw.close(); } }
Similar Threads
-
Extracting values from textfile with semi-random content
By Kenjitsuka in forum New To JavaReplies: 9Last Post: 03-02-2011, 11:52 PM -
need to input values from a text file into an array and count values
By pds8475 in forum New To JavaReplies: 14Last Post: 01-22-2011, 02:36 PM -
Extracting and displaying data from a text file
By fugazi in forum New To JavaReplies: 4Last Post: 01-07-2011, 06:37 PM -
How to Read data from text file and calculate the values?
By janeansley in forum New To JavaReplies: 40Last Post: 07-04-2008, 08:41 AM -
[SOLVED] getting values from a text file
By dav9999 in forum New To JavaReplies: 8Last Post: 04-01-2008, 01:51 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks