JAVA: In main, I am processing a file input that has a String employeeNumber fol
ID: 3823326 • Letter: J
Question
JAVA: In main, I am processing a file input that has a String employeeNumber followed by a series (1 - 3) of int jobNumber on the same line. Each line needs to be processed and displayed to a report in a different JobPrint class where it will be printed to a report. I need the code that will process the String item and the int items in such a way that it can be printed to a report.
Example input:
smith444 3974 2238 2222
davis222 4567
john777 2121 3434
Example code:
public static void main(String[] args) {
File inputDataFile = new File(JOB_REPORT);
Scanner dataInput = new Scanner(inputDataFile);
String [] jobReport = new String [100]; // array of 100 does not mean anything
while(dataInput.hasNextLine()){
String aLine = dataInput.nextLine();
jobReport = aLine.split(" ");
/*
I don't know what to do here!
*/
}
}
public class JobPrint(){
//empty constructor but does not have to be
public JobPrint(){
}
public PrintToFile( // some parameter){
File outputDataFile = new File(JOB_REPORT_PROCESSED);
PrintWriter jobOutput = new PrintWriter(outputDataFile);
/*
I don't know what goes here.
/*
}
}
Explanation / Answer
public static void main(String[] args) {
File inputDataFile = new File(JOB_REPORT);
Scanner dataInput = new Scanner(inputDataFile);
Integer[] job1 = new Integer [100];
Integer[] job2 = new Integer [100];
Integer[] job3 = new Integer [100];
String[] emp = new String [100];
String [] jobReport = new String [100]; // array of 100 does not mean anything
int count = 0;
while(dataInput.hasNextLine()){
String aLine = dataInput.nextLine();
jobReport = aLine.split(" ");
empName[count] = jobReport[0]; //first field of emp name and emp number
job1[count] = Integer.parseInt(jobReport[1]); //store job1 which is compulsory field
if (jobReport.length >= 3) //with out this we would could face exception such as ArrayIndexOutOfBound Exception
job2[count] = Integer.parseInt(jobReport[2]);
if (jobReport.length >= 4)
job3[count] = Integer.parseInt(jobReport[3]);
count ++;
}
PrintToFile(empName, job1, job2, job3);
}
public class JobPrint(){
//empty constructor but does not have to be
public JobPrint(){
}
public PrintToFile( String[] name, Integer[] j0, Integer[] j1, Integer[] j2){
File outputDataFile = new File(JOB_REPORT_PROCESSED);
PrintWriter jobOutput = new PrintWriter(outputDataFile);
for (int i = 0; i < name.length; i++) {
if (name[i] == null) //indicates end of employee in the list
break;
jobOutput.println(name[i] + " " j0[i] + " " j1[i] + " " j2[i] );
}
jobOutput.flush();
outputDataFile.close();
}
}
I tried to keep things simple and hope you like it. If you are facing any trouble regarding the code or logic please feel free to comment below. I shall be glad to help you with the same
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.