Write an application that allows you to enter any ID number and displays the first and last name for the record stored in the employee file with the given ID number. Display an appropriate message if the ID number cannot be found in the input file.
This is based on a comma-delimited text file that has already been created containing a 3 digit ID # and a first and last name. The last part of the excercise is as follows:
Write an application that allows you to enter any first name and displays all the
ID numbers and last names for any records stored in the employee file with the
given first name. Display an appropriate message if the first name cannot be
found in the input file.
Here's what I've got so far. It only displays the ID# and 000, , :
import java.nio.file.*;
import java.nio.channels.FileChannel;
import java.nio.ByteBuffer;
import static java.nio.file.StandardOpenOption.*;
import java.util.Scanner;
public class DisplaySelectedIdNumbers {
public static void main(String[] args) {
Scanner keyBoard = new Scanner(System.in);
Path file
= Paths.get("C:\\Users\\Jeremiah\\Documents\\Test\\writeFile.txt");
String s = "000, , "
+ System.getProperty("line.separator");
final int RECSIZE = s.length();
byte[] data = s.getBytes();
ByteBuffer buffer = ByteBuffer.wrap(data);
FileChannel fc = null;
String idString;
int id;
try {
fc = (FileChannel) Files.newByteChannel(file, READ, WRITE);
System.out.print("Enter employee ID number ");
idString = keyBoard.nextLine();
id = Integer.parseInt(idString);
buffer = ByteBuffer.wrap(data);
fc.position(id * RECSIZE);
fc.read(buffer);
s = new String(data);
System.out.println("ID #" + id + " " + s);
fc.close();
} catch (Exception e) {
System.out.println("Error message: " + e);
}
}
}