in java how can i take the data from these arrays and store them to a text file,
ID: 3700727 • Letter: I
Question
in java how can i take the data from these arrays and store them to a text file, as well as, search keywords in the data as a user depending on if I am searching for jobs, employers or applicants and displays the results to screen.
Employers[] emp = new Employers[5];
Applicants[] app = new Applicants[5];
Jobs[] job = new Jobs[5];
// create employee
emp[0] = new Employers("Nath", "Muellner", "nmuellner@technopark.com", "1-213-233-9474", 37, 10, "LA",
"TechnoPark", "HR");
emp[1] = new Employers("Mark", "Jamison", "mjamison@itpark.com", "1-213-262-7463", 35, 5, "LA", "ITPark",
"Manager");
emp[2] = new Employers("Paul", "Gregory", "pgregory@technicalinnovations.com", "1-213-343-2312", 43, 12, "LA",
"Technical Innovations", "Manager");
emp[3] = new Employers("Harrison", "Jones", "jharrison@sebring.com", "1-213-234-4332", 34, 4, "LA",
"Sebring Techonologies", "HR");
emp[4] = new Employers("Chris", "Lawrence", "clawrence@createch.com", "1-213-231-3532", 29, 2, "LA", "Createch",
"Manager");
// create applicants
app[0] = new Applicants("Samantha", "Michaels", "smichaels@hotmail.com", "1-213-212-2321", 30, "BS", 5, "LA",
"Programmer");
app[1] = new Applicants("George", "Peterson", "gpeterson@yahoo.com", "1-212-434-2322", 24, "MBA", 3, "LA",
"Manager");
app[2] = new Applicants("Katie", "Harris", "kharris@hotmail.com", "1-213-232-1112", 28, "BS", 5, "LA",
"Database Administrator");
app[3] = new Applicants("Kevin", "Reynolds", "kreynolds78@hotmail.com", "1-213-653-2313", 39, "MS", 12, "LA",
"Lead Programmer");
app[4] = new Applicants("Renee", "Collins", "rcollins@aol.com", "1-213-456-4352", 47, "MS", 14, "LA", "CEO");
Create job postings
job[0] = new Jobs("Technical Innovations", "pgregory@technicalinnovations.com", 75000.23, 5, "LA", "Manager");
job[1] = new Jobs("Sebring Technologies", "jharrison@sebring.com", 100000.40, 5, "LA", "Lead Programmer");
job[2] = new Jobs("IT Park", "mjamison@itpark.com", 85000.23, 3, "LA", "Database Administrator");
job[3] = new Jobs("Createch", "clawrence@createch.com", 70000.24, 3, "LA", "Network Administrator");
job[4] = new Jobs("TechnoPark", "nmuellner@technopark.com", 80000.00, 2, "LA", "Manager");
Explanation / Answer
Here is your solution:
Employers.java
/**
* @author Lokesh Kumar
*
*/
public class Employers {
private String firstName;
private String lastName;
private String email;
private String number;
private int age;
private int experience;
private String city;
private String company;
private String role;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
/**
* @param firstName
* @param lastName
* @param email
* @param number
* @param age
* @param experience
* @param city
* @param company
* @param role
*/
public Employers(String firstName, String lastName, String email, String number, int age, int experience,
String city, String company, String role) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.number = number;
this.age = age;
this.experience = experience;
this.city = city;
this.company = company;
this.role = role;
}
}
Applicants.java
/**
* @author Lokesh Kumar
*
*/
public class Applicants {
private String firstName;
private String lastName;
private String email;
private String number;
private int age;
private String qualification;
private int experience;
private String city;
private String role;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getQualification() {
return qualification;
}
public void setQualification(String qualification) {
this.qualification = qualification;
}
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
/**
* @param firstName
* @param lastName
* @param email
* @param number
* @param age
* @param qualification
* @param experience
* @param city
* @param role
*/
public Applicants(String firstName, String lastName, String email, String number, int age, String qualification,
int experience, String city, String role) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.number = number;
this.age = age;
this.qualification = qualification;
this.experience = experience;
this.city = city;
this.role = role;
}
}
Jobs.java
/**
* @author Lokesh Kumar
*
*/
public class Jobs {
private String company;
private String email;
private double salary;
private int experience;
private String city;
private String role;
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getRole() {
return role;
}
public int getExperience() {
return experience;
}
public void setExperience(int experience) {
this.experience = experience;
}
public void setRole(String role) {
this.role = role;
}
/**
* @param company
* @param email
* @param salary
* @param experience
* @param city
* @param role
*/
public Jobs(String company, String email, double salary, int experience, String city, String role) {
super();
this.company = company;
this.email = email;
this.salary = salary;
this.experience = experience;
this.city = city;
this.role = role;
}
}
TestMain.java
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
/**
* @author Lokesh Kumar
*
*/
public class TestMain {
public static void storeData(Employers[] emp, Applicants[] app, Jobs[] job) {
File dataFile;
BufferedWriter writer;
try {
dataFile = new File("E://Data.txt");
if (!dataFile.exists()) {
dataFile.createNewFile();
} else {
dataFile.delete();
dataFile.createNewFile();
writer = new BufferedWriter(new FileWriter(dataFile, true));
int i = 0;
for (Employers employers : emp) {
if (i > 0) {
writer.newLine();
}
writer.write("Employer," + employers.getFirstName() + "," + employers.getLastName() + ","
+ employers.getEmail() + "," + employers.getNumber() + "," + employers.getAge() + ","
+ employers.getExperience() + "," + employers.getCity() + "," + employers.getCompany() + ","
+ employers.getRole());
i++;
}
for (Applicants applicants : app) {
if (i > 0) {
writer.newLine();
}
writer.write("Applicant," + applicants.getFirstName() + "," + applicants.getLastName() + ","
+ applicants.getEmail() + "," + applicants.getNumber() + "," + applicants.getAge() + ","
+ applicants.getQualification() + "," + applicants.getExperience() + ","
+ applicants.getCity() + "," + applicants.getRole());
}
for (Jobs jobs : job) {
if (i > 0) {
writer.newLine();
}
writer.write("Job," + jobs.getCompany() + "," + jobs.getEmail() + "," + jobs.getSalary() + ","
+ jobs.getExperience() + "," + jobs.getCity() + "," + jobs.getRole());
}
writer.close();
}
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
public static void search(String category, String keyWord) {
FileReader fr;
BufferedReader br;
String[] data = null;
try {
fr = new FileReader("E://Data.txt");
br = new BufferedReader(fr);
String line;
int j = 0;
int count = 1;
data = new String[90];
while ((line = br.readLine()) != null) {
String[] array = line.split(",");
if (category.equalsIgnoreCase((array[0]))) {
if (line.contains(keyWord)) {
data[j] = array[0] + " : " + count;
for (int i = 1; i < array.length; i++) {
j = j + 1;
data[j] = array[i];
}
count = count + 1;
}
}
}
JFrame f = new JFrame("Output");
JPanel panel = new JPanel();
JScrollPane scroll = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
panel.add(new JList(data));
f.setSize(400, 400);
f.add(scroll);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
br.close();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
}
public static void main(String[] args) {
Employers[] emp = new Employers[5];
Applicants[] app = new Applicants[5];
Jobs[] job = new Jobs[5];
// create employee
emp[0] = new Employers("Nath", "Muellner", "nmuellner@technopark.com", "1-213-233-9474", 37, 10, "LA",
"TechnoPark", "HR");
emp[1] = new Employers("Mark", "Jamison", "mjamison@itpark.com", "1-213-262-7463", 35, 5, "LA", "ITPark",
"Manager");
emp[2] = new Employers("Paul", "Gregory", "pgregory@technicalinnovations.com", "1-213-343-2312", 43, 12, "LA",
"Technical Innovations", "Manager");
emp[3] = new Employers("Harrison", "Jones", "jharrison@sebring.com", "1-213-234-4332", 34, 4, "LA",
"Sebring Techonologies", "HR");
emp[4] = new Employers("Chris", "Lawrence", "clawrence@createch.com", "1-213-231-3532", 29, 2, "LA", "Createch",
"Manager");
// create applicants
app[0] = new Applicants("Samantha", "Michaels", "smichaels@hotmail.com", "1-213-212-2321", 30, "BS", 5, "LA",
"Programmer");
app[1] = new Applicants("George", "Peterson", "gpeterson@yahoo.com", "1-212-434-2322", 24, "MBA", 3, "LA",
"Manager");
app[2] = new Applicants("Katie", "Harris", "kharris@hotmail.com", "1-213-232-1112", 28, "BS", 5, "LA",
"Database Administrator");
app[3] = new Applicants("Kevin", "Reynolds", "kreynolds78@hotmail.com", "1-213-653-2313", 39, "MS", 12, "LA",
"Lead Programmer");
app[4] = new Applicants("Renee", "Collins", "rcollins@aol.com", "1-213-456-4352", 47, "MS", 14, "LA", "CEO");
// Create job postings
job[0] = new Jobs("Technical Innovations", "pgregory@technicalinnovations.com", 75000.23, 5, "LA", "Manager");
job[1] = new Jobs("Sebring Technologies", "jharrison@sebring.com", 100000.40, 5, "LA", "Lead Programmer");
job[2] = new Jobs("IT Park", "mjamison@itpark.com", 85000.23, 3, "LA", "Database Administrator");
job[3] = new Jobs("Createch", "clawrence@createch.com", 70000.24, 3, "LA", "Network Administrator");
job[4] = new Jobs("TechnoPark", "nmuellner@technopark.com", 80000.00, 2, "LA", "Manager");
TestMain.storeData(emp, app, job);
String[] choices = { ("Job"), "Applicant", "Employer" };
// Use combobox to create drop down menu
JComboBox comboBox = new JComboBox(choices);
JPanel panel1 = new JPanel(new FlowLayout());
JLabel label1 = new JLabel("Select Categoty:");
panel1.add(label1);
panel1.add(comboBox);
JButton button = new JButton("Search");
JLabel label2 = new JLabel("Keyword:");
JTextField text = new JTextField(20);
// Create a JFrame that will be use to put JComboBox into it
JFrame frame = new JFrame("Search");
frame.setLayout(new FlowLayout()); // set layout
frame.add(panel1);
frame.add(label2);
frame.add(text);
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String category = String.valueOf(comboBox.getSelectedItem());
String keyWord = text.getText();
TestMain.search(category, keyWord);
}
});
frame.add(button);
// set default close operation for JFrame
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// set JFrame ssize
frame.setSize(250, 250);
// make JFrame visible. So we can see it
frame.setVisible(true);
}
}
Explanation:-
Here I'm taking three classes for Employers,Applicants,Jobs. And finally the main class which is to create a file and search a file with given keyword.
First i'm taking Arrays what you have mentioned in this Question, and by using storeData method in TestMain class i'm inserting All Arrays into file line by line.
And after that asking for search keyword Employer , Applicant, Job
Based upon user selection It'll print out all data from a file.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.