Write an application that allows you to enter any ID number and displays the fir
ID: 3557734 • Letter: W
Question
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);
}
}
}
Explanation / Answer
As per the Application Requirement I Have taken Example that uses both FileWrite.java and FileRead.java
// FileWrite.java
/* input the some data as " Id firstname lastname " */
import java.io.*;
import java.util.*;
public class FileWrite
{
public static void main(String[] args) throws Exception
{
String fname="",lname="";
String id = "";
FileWriter fw=new FileWriter(new File("input.txt"),true);
BufferedWriter bw=new BufferedWriter(fw);
Scanner sc = new Scanner(System.in);
System.out.println("Enter Number of Records into the File");
int in = sc.nextInt();
for(int i = 1; i <= in; i++)
{
System.out.println("Enter "+i+"Id");
id = sc.next();
System.out.println("Enter First Name");
fname = sc.next();
System.out.println("Enter Last Name");
lname = sc.next();
bw.write(id);
bw.write(' ');
bw.write(fname);
bw.write(' ');
bw.write(lname);
bw.write(' ');
bw.newLine();
}
bw.flush();
bw.close();
}
}
// FileRead.java
/* WAP that if you enter Id displays the First Name and Last Name */
import java.io.*;
import java.util.*;
public class FileRead
{
public static void main(String[] args) throws Exception
{
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
String s=br.readLine();
Scanner sc = new Scanner(System.in);
System.out.println("Enter ID ");
String id =sc.next();
int count = 0;
while(s!=null)
{
String []arr = s.split(" ");
if(id.equals(arr[0]))
{
System.out.println(arr[1]+" "+arr[2]);
count++;
}
s=br.readLine();
}
if(count == 0)
System.out.println("Entered ID Not Found ");
br.close();
}
}
// FileRead2.java
/* WAP that if you enter First Name and it Displays the ID and Last Name */
import java.io.*;
import java.util.*;
public class FileRead2
{
public static void main(String[] args) throws Exception
{
// Here takes input.txt as input file
FileReader fr=new FileReader("input.txt");
BufferedReader br=new BufferedReader(fr);
String s=br.readLine();
Scanner sc = new Scanner(System.in);
System.out.println("Enter First Name ");
String id =sc.next();
int count = 0;
while(s!=null)
{
String []arr = s.split(" ");
if(id.equals(arr[1]))
{
System.out.println(arr[0]+" "+arr[2]);
count++;
}
s=br.readLine();
}
if(count == 0)
System.out.println("First Name Not Found ");
br.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.