Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Problem with code for school assignment?

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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:
    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
    Last edited by helloworld922; December 16th, 2010 at 09:31 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Problem with code for school assignment?

    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.

    Employee [] employees = getEmployees(); // getEmployees is a rename of your method allE
    for(int i = 0; i<a.length; i++)
    {
        pw.println(a[i]);
    }

Similar Threads

  1. Assignment problem.
    By minou13 in forum Java Theory & Questions
    Replies: 1
    Last Post: November 26th, 2010, 10:51 PM
  2. school project
    By robin28 in forum Java Theory & Questions
    Replies: 13
    Last Post: November 12th, 2010, 09:11 AM
  3. School Help
    By JavaNewb in forum Loops & Control Statements
    Replies: 1
    Last Post: March 24th, 2010, 09:31 PM
  4. Java program for to implement supermarket menu
    By 5723 in forum AWT / Java Swing
    Replies: 1
    Last Post: April 14th, 2009, 03:14 AM
  5. How to use for loop for movement of points in a picture display?
    By Dman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2009, 09:19 AM