Problem with code for school assignment?
I am having a problem with one part of my program i have to "have a method named "public void generateEmployees(int howmany)". In this method, based
# on the value of the input parameter "howmany", you create "howmany" employees and store them in # the variable "allE" and have a method named "public void storeInfo(String filename)" in which you write out all employee information into a file
" So far I have:
Code Java:
private final Employee[] allE()
{
return allE();
}
public void generateEmployees(int howmany)
{
Employee[] allE = new Employee[howmany];
for(int i = 0; i<howmany; i++)
{
allE[i] = new Employee(Generator.generateName(5), Generator.generateName(4), Generator.generateSSN(), Generator.generateID(6));
allE[i].print();
}
}
public void storeInfo(String filename)throws IOException
{
File file = new File(filename);
if (!file.exists())
{
System.out.println("File does not exist");
System.exit(1);
}
PrintWriter pw = new PrintWriter(filename);
for(int i = 0; i<allE().length; i++)
{
Employee [] a = allE();
pw.print(a);
//*** i think that i am missing a part here!
}
pw.close();
}
I am sorry i do not know much about java any help at all would be greatly appreciated! thank you so much to all who respond!!!
I am having a problem with this part:
You have a method named "public void storeInfo(String filename)" in which you write out all employee information into a file
Re: Problem with code for school assignment?
Code Java:
for(int i = 0; i<allE().length; i++)
{
Employee [] a = allE();
pw.print(a);
}
Normally you want to get the array of all employees outside the loop, then print out each employee information individually. Also, unless your toString() method is defined to print out the information with a newline at the end (it shouldn't), you should probably use the println() method.
I would also suggest picking better names for your methods and variables. Method names such as allE() make little sense to everyone else or what it does, and variable names such as a give no indication as to what it's holding.
Code Java:
Employee [] employees = getEmployees(); // getEmployees is a rename of your method allE
for(int i = 0; i<a.length; i++)
{
pw.println(a[i]);
}