Results 1 to 5 of 5
  1. #1
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default 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:

    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();
        }	
        	 
    }
    And produces the following output (ex. if 1 is chosen )
    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 !!!

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    You can use the SimpleDateFormat and Date classes

  3. #3
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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!

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by kryptonian03 View Post
    I dont know how to use SimpleDateFormat.... Would please give me a sample snippet for it?
    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.

    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!
    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).

  5. #5
    kryptonian03 is offline Member
    Join Date
    Dec 2010
    Posts
    18
    Rep Power
    0

    Default

    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

  1. Replies: 9
    Last Post: 03-02-2011, 11:52 PM
  2. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  3. Extracting and displaying data from a text file
    By fugazi in forum New To Java
    Replies: 4
    Last Post: 01-07-2011, 06:37 PM
  4. Replies: 40
    Last Post: 07-04-2008, 08:41 AM
  5. [SOLVED] getting values from a text file
    By dav9999 in forum New To Java
    Replies: 8
    Last Post: 04-01-2008, 01:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •