hi there hoping someone can help me out with this.
im designing a code that is a basic database holding peoples details and i need to implement a method that searches by txt for a record by their last name.
this is the code i have for reading the data in. i dont know how i can go about coding a search method. hopefully someone can help me thanks.
im designing a code that is a basic database holding peoples details and i need to implement a method that searches by txt for a record by their last name.
Code:
import java.util.*;
import java.io.PrintStream;
import java.io.File;
import java.io.FileNotFoundException;
public class coursework4
{
private static final int MAX_RECORDS = 20;
private static String lastName[] = new String[MAX_RECORDS];
private static String firstName[] = new String[MAX_RECORDS];
private static String telNumber[] = new String[MAX_RECORDS];
private static String emailAddress[] = new String[MAX_RECORDS];
private static Scanner data_input = new Scanner(System.in);
public static int read_in_file(String file_name)
{
Scanner read_in;
Scanner line;
int record_count = 0;
String record;
try
{
read_in = new Scanner(new File(file_name));
// read in one line at a time
read_in.useDelimiter(System.getProperty("line.separator"));
while (read_in.hasNext())
{
// Test to see if there are too many records in the file
if (record_count == MAX_RECORDS)
{
System.out.printf("Only %d records allowed in file", MAX_RECORDS);
System.exit(0);
}
// read in record
record = new String(read_in.next());
// Split the record up into its fields and store in
// appropriate arrays
line = new Scanner(record);
line.useDelimiter("\\s*,,\\s*");
lastName[record_count] = line.next();
firstName[record_count] = line.next();
telNumber[record_count] = line.next();
emailAddress[record_count] = line.next();
// Increment record count
record_count++;
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
return record_count;
}
public static void write_out_file(int no_of_records, String filename)
{
PrintStream write_out;
int counter;
try
{
// Create new file
write_out = new PrintStream(new File(filename));
// Output all reacords
for(counter = 0; counter < no_of_records; counter++)
{
// Output a record
write_out.print(lastName[counter]);
write_out.print(",,");
write_out.print(firstName[counter]);
write_out.print(",,");
write_out.print(telNumber[counter]);
write_out.print(",,");
write_out.println(emailAddress[counter]);
}
// Close file
write_out.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
Comment