Get data from notepad and display it.

Asked By 10 points N/A Posted on -
qa-featured

I need a little help in java programming. I will be using in my payroll system. I need a program in java who will let the user enter an employee code then all of the information about the employee having that code will appear. The program will get the information from a notepad file.

Example:

Enter Employee code: 110011

(If I enter the code the information of the employee who have that code will appear)

Employee name: John S.

Age:20

Position:Secretary

Rate per hour: 5.00$

Country: USA

I'll just want a sample code as my reference. Thanks

SHARE
Answered By 5 points N/A #101949

Get data from notepad and display it.

qa-featured

Hi Smith,

Here is a very simple solution for you.

 

import java.io.*;
class text_reader 
{
 public static void main(String args[])
 {
 
     try{
 
        // open the text file
        // you can specify a full path if the file 
        // is not located on the same folder
         FileInputStream fstream = new FileInputStream("textfile.txt");
         // inialize input stream
         DataInputStream dstream = new DataInputStream(fstream);
         BufferedReader reader = new BufferedReader(new InputStreamReader(dstream));
         String strLine;
         String fields [];
         fields = new String[6];
         // fields
         fields[0] = "Employee Code";
         fields[1] = "Employee name";
         fields[2] = "Age";
         fields[3] = "Position";
         fields[4] = "Rate per hour";
         fields[5] = "Country";
         System.out.println("Enter employee code:");
         InputStreamReader input = new InputStreamReader(System.in);
         BufferedReader inputreader = new BufferedReader(input);
         String sEmpSearchCode = inputreader.readLine();
         boolean bFound = false; 
         //read File Line By Line
         while ((strLine = reader.readLine()) != null)
         {
        
             // tokenize the string, match 1st element with our search string
             String[] tokens = strLine.split("\,");
             String sEmpCode = tokens[0];
             if(sEmpCode.compareTo(sEmpSearchCode) == 0)
             {
                 // we found it
                 // print it
                 bFound = true;
                 for (int index=0; index<tokens.length; index++)
                 System.out.println(fields[index] + " = " + tokens[index]);
                 break;
             }
            
         }
            //close the input stream
            dstream.close();
       
        if(!bFound)
            System.out.println( sEmpSearchCode + " not found");
        }
        catch (Exception e){//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }
}
 
Create a textfile named "textfile.txt" on the same folder with your java program. The format is:
 
110011,John Shy.,20,Secretary,5.00$,USA
110100,Jon Doe,22,VP,100.00$,USA
 
The program reads it line by line and split the data using comma. 
 
I hope it helps.

Related Questions