Question:
Simple java, fetch data from notepad or text file?
TheNiceGuy
2011-02-18 00:04:36 UTC
i have a simple program which fetch the data from a notepad called "employees.txt"
inside is:

EMP-001 , Paul Flores, DBA , 500
EMP-002 , Rio Reyes , PROG , 450
EMP-003 , Kate Smith , PROG , 450
EMP-004 , Suzanne Moretz , PROG , 300

now, i want to fetch the record. if i typed EMP-001, it will select the row from it, and the output must be,

EMPLOYEE______POSITION_____RATE PER HOUR
Paul Flores ------------ DBA --------------------500

its a simple program but i cant get it right.. please give the codes..

NOTE: its not a GUI, it will only display in the output box..
10pts for the best answer!!
Three answers:
deonejuan
2011-02-18 00:38:37 UTC
import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Scanner;



public class EmployeeInfo {



public static void main(String[] args) throws IOException {

FileReader fin = new FileReader("employees.txt");

Scanner sc = new Scanner(fin);

ArrayList al = new ArrayList();

while (sc.hasNext()) {

al.add( new Employee( sc.nextLine()));

}

fin.close();

sc.close();

sc = new Scanner( System.in );

System.out.print("Enter Employee ID => ");

String id = sc.nextLine();

Employee searchEmp = null;

for (Employee employee : al) {

if( id.equalsIgnoreCase( employee.getID())) {

searchEmp = employee;

break;

}

}

if( searchEmp != null ) {

System.out.println("EMPLOYEE\tPOSITION\tRATE PER HOUR");

System.out.println(searchEmp);

}

}



private static class Employee {

String[] fields;

public Employee( String data ){

fields = data.split(", ");

}

public String getID( ) {

return fields[0];

}



public String toString() {

String info = String.format("%22s",fields[1]);

info += String.format("%12s",fields[2]);

info += String.format("%s",fields[3]);



return info;

}

}

}



/** notes:

this is as simple as I can make it. Your employees.txt file should be in the same folder as the EmployeeInfo.class file. Your data file is inconsistent. sometimes you have id [space][comma][space] and sometimes you have id[comma][space].
butlin
2016-12-12 09:17:40 UTC
i'm not springing up the carried out utility yet it incredibly is what does it: gadget.out.println("Poem starts off herenPoem starts off back suitable right here."); could output: Poem starts off suitable right here Poem starts off back suitable right here. Metaprogramming suitable right here so bare with me, yet in elementary terms pay interest to the " n " as that's what creates the line after the code.
gattus
2016-11-30 02:46:56 UTC
i'm no longer springing up the accomplished utility yet that's what does it: gadget.out.println("Poem starts off herenPoem starts off decrease back impressive right here."); might desire to output: Poem starts off impressive right here Poem starts off decrease back impressive right here. Metaprogramming impressive right here so bare with me, yet in basic terms pay interest to the " n " as that's what creates the line after the code.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...