JAVA: Create a text file that has a list of people and their birthdays. It shoul
ID: 3709325 • Letter: J
Question
JAVA:
Create a text file that has a list of people and their birthdays.
It should contain the first name, the last name and the birth day in mm dd yyyy format.
Example is:
Joe Smith 12 28 1999
Mary Jane 11 02 1998
Create a .java file that reads in this file into an array. Make the array of size 100.
In a loop, ask the user for their birthdate. Check to see if there is a match with anyone in the array. If there is a match let the user know who it is. If there is no match let the user know there was no match.
Explanation / Answer
LoadBirthdayData.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LoadBirthdayData {
static int n = 0;
public static void main(String[] args) throws FileNotFoundException {
String loadArray[] = loadData();
Scanner scan = new Scanner(System.in);
System.out.println("Enter the birthday: ");
String birthday = scan.next();
String str[] = birthday.split("/");
boolean found = false;
int index = -1;
for(int i=0;i<n;i++) {
String s = loadArray[i];
String data[] = s.split("\s+");
if(data[2].equals(str[0])&&data[3].equals(str[1])&&data[4].equals(str[2])) {
found = true;
index = i;
break;
}
}
if(found) {
System.out.println(loadArray[index]);
} else {
System.out.println("Record not found");
}
}
public static String[] loadData() throws FileNotFoundException {
File file = new File("D:\data.txt");
if(file.exists()) {
Scanner scan = new Scanner(file);
String s[] = new String[100];
while(scan.hasNextLine()) {
s[n++]=scan.nextLine();
}
return s;
}else {
System.out.println("File does not exit");
return null;
}
}
}
Output:
Enter the birthday:
12/28/1999
Joe Smith 12 28 1999
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.