In Java, how can I make the below code say invalid input please try again to off
ID: 3701075 • Letter: I
Question
In Java, how can I make the below code say invalid input please try again to offer user another chance if they do not put in applicant, employer or job for keyword search?
TestMain.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
/**
* @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 keyWord) {
FileReader fr;
BufferedReader br;
try {
fr = new FileReader("E://Data.txt");
br = new BufferedReader(fr);
String line;
int j=1;
while ((line = br.readLine()) != null) {
String[] array = line.split(",");
if (keyWord.equalsIgnoreCase((array[0]))) {
System.out.println("----------------------------------");
System.out.println(array[0] + " : "+j);
for (int i = 1; i < array.length; i++) {
System.out.println(array[i]);
}
j=j+1;
}
}
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);
System.out.println("Enter a keyword to search ");
System.out.println("Employer Applicant Job ");
Scanner scanner = new Scanner(System.in);
String keyWord = scanner.next();
TestMain.search(keyWord);
scanner.close();
}
}
Explanation / Answer
All you have to do is add few lines in your main method.
One way to do that is to change return type of your TestMain.search method, add a do while loop in your main method and change your code a little bit in TestMain.search method.
Following code explains it:
import java.util.Scanner;
public class TestMain{
private static boolean search(String keyword){
if(keyword.equalsIgnoreCase("Employer") || keyword.equalsIgnoreCase("Applicant")||keyword.equalsIgnoreCase("Job")){
//your search logic
return true;
}else{
System.out.println("Wrong search Try again");
return false;
}
}
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String keyWord;
do{
System.out.println("Enter a keyword to search ");
System.out.println("Employer Applicant Job ");
keyWord = scanner.next();
}while(!TestMain.search(keyWord));
scanner.close();
}
}
Following is the full code:
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
/**
* @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 boolean search(String keyWord) {
if(!(keyword.equalsIgnoreCase("Employer") || keyword.equalsIgnoreCase("Applicant")||keyword.equalsIgnoreCase("Job"))){
System.out.println("Wrong Keyword. Try again");
return false;
}
FileReader fr;
BufferedReader br;
try {
fr = new FileReader("E://Data.txt");
br = new BufferedReader(fr);
String line;
int j=1;
while ((line = br.readLine()) != null) {
String[] array = line.split(",");
if (keyWord.equalsIgnoreCase((array[0]))) {
System.out.println("----------------------------------");
System.out.println(array[0] + " : "+j);
for (int i = 1; i < array.length; i++) {
System.out.println(array[i]);
}
j=j+1;
}
}
br.close();
} catch (Exception exception) {
System.out.println(exception.getMessage());
}
return true;
}
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);
Scanner scanner = new Scanner(System.in);
String keyWord;
do{
System.out.println("Enter a keyword to search ");
System.out.println("Employer Applicant Job ");
keyWord = scanner.next();
}while(!TestMain.search(keyWord));
scanner.close();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.