0

I'm getting multiple erros with this part of my program. What it basically does is read input from an unorganized file, PersonnelInfo.txt, and it sorts it into an array in the EmployeeInfo class (not posted because I don't believe the problem lies in that class, if you guys want, I will post it for reference).

After sorting the info (Like Name, ID number, title at the company, etc.) the program writes this to another file, ORGANIZEDPersonnelInfo.txt, which will be sorted as such:

Name: Employee name here

ID: Employee ID here

etc.

One error I am getting is in the "static FileWriter writeOut..." line and it is "Unhandled exception type IOException. I put a throws IOException in the main method declaration but it did nothing.

Next, in the "public String toString()" method I am receiving multiple errors. When I try to make it void, it says the return type is incompatible with Object.toString(). When I keep it as a String and return null it wants me to use try/catch but the main error still exists.

Please help! This is getting pretty frustrating...

import java.io.*;
import java.util.Scanner;

public class HR {

static EmployeeInfo[] employee = new EmployeeInfo[4];
static FileWriter writeOut = new FileWriter("/Users/zach/Documents/workspace/Dec. 10, 2012/src/ORAGNIZEDPersonnelInfo.txt");
static BufferedWriter out = new BufferedWriter(writeOut);


public static void main(String[] args) throws IOException {
    File PersonnelInfo = new File("/Users/zach/Documents/workspace/Dec. 10, 2012/src/PersonnelInfo.txt");
    Scanner inputFile = new Scanner(PersonnelInfo);

    int numOfEmployees = Integer.parseInt(inputFile.nextLine());
    System.out.println("Number of employees: " + numOfEmployees);



    for (int i = 0; i < 4; i++) {
        String name = inputFile.nextLine();
        String ID = inputFile.nextLine();
        String title= inputFile.nextLine();
        String startDate = inputFile.nextLine();
        employee[i] = new EmployeeInfo(name, ID, title, startDate);
    }

    listEmployeeInfo();
    employee.toString();

}


public String toString() {
    for (EmployeeInfo i: employee) {
        out.write("Name: " + i.getName());
        out.write("ID: " + i.getID());
        out.write("Title: " + i.getTitle());
        out.write("Start Date: " + i.getStartDate());
    }
    return null;
}
Zach
  • 4,555
  • 9
  • 31
  • 52

5 Answers5

0

Corrected toString()

public String toString() {
    StringBuffer buf = new StringBuffer();
    for (EmployeeInfo i: employee) {
        buf.append("Name: ").append(i.getName());
        buf.append("ID: ").append(i.getID());
        // ....
    }
    return buf.toString();
}

in main you print the content:

HR hr = new HR();
out.write(hr.toString());

But I would write using an own write method "writeEmployees(out)" see Solution of poster JB Nizet

AlexWien
  • 28,470
  • 6
  • 53
  • 83
0

If you override toString() (as you're doing, since toSTring() is a method from Object, and you're redefining it in your class, which extends Object as all classes do), you should respect the contract of the overridden method, which is to return a String representation of the object (and not to write fields to a buffered writer. Name this method with another name.

Moreover, you're calling a method (out.write()) which throws a checked exception: IOException. So, either you know how to handle this exception, and you should catch it, or you don't know how to handle it, and the method should declare that it throws the exception:

public void writeToOut() throws IOException {
    for (EmployeeInfo i: employee) {
        out.write("Name: " + i.getName());
        out.write("ID: " + i.getID());
        out.write("Title: " + i.getTitle());
        out.write("Start Date: " + i.getStartDate());
    }
}

You have other compilation errors in your program. Each one comes with an error message, a file name and a line number. Try to understand the message, and to fix the error. Read the Java tutorial about exceptions to understand how to handle them.

If you still can't handle them after having read this tutorial, ask another question and paste the exact error message you get from the compiler.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
0

I would advice to move your static declaration of Writer inside your main for two reasons (one they don't need to be a static attributes and also the IOException is being handled there). Get the string from toString and use the writer to write the string in the file:

  public class HR {

    static EmployeeInfo[] employee = new EmployeeInfo[4];


    public static void main(String[] args) throws IOException {
         FileWriter writeOut = 
                    new FileWriter("/Users/zach/Documents/workspace/"+
                                   "Dec. 10, 2012/src/ORAGNIZEDPersonnelInfo.txt");
         BufferedWriter out = new BufferedWriter(writeOut);

         File PersonnelInfo = new File("/Users/zach/Documents/workspace/"+
                                       "Dec. 10, 2012/src/PersonnelInfo.txt");
         Scanner inputFile = new Scanner(PersonnelInfo);

         ....
         //write employee string in the file
         out.write(getEmployeeString());
         out.close();
    }

    public String getEmployeeString() {
     StringBuilder stringBuilder = new StringBuilder();
     for (EmployeeInfo i: employee) {
          stringBuilder.append("Name: " + i.getName());
          stringBuilder.append("ID: " + i.getID());
          stringBuilder.append("Title: " + i.getTitle());
          stringBuilder.append("Start Date: " + i.getStartDate());
     }
     return stringBuilder.toString();
   }       
 }

If you must need to make them as static variables then do the initialization in a static block with exception handling as below:

     static FileWriter writeOut;
     static BufferedWriter out;

     static {
        try{
           writeOut = 
                    new FileWriter("/Users/zach/Documents/workspace/"+
                                   "Dec. 10, 2012/src/ORAGNIZEDPersonnelInfo.txt");
              out = new BufferedWriter(writeOut);
         }catch(IOException ioex){
               ioex.printStackTrace();
         }
     } 
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
  • Thanks a ton, this solution gets the program writing to the file, but now it's writing [LEmployeeInfo;@39617189, so I need to figure out what's going on. – Zach Nov 30 '12 at 21:56
  • @Zach Its simple error from my side. Instead of `out.write(employee.String());`(which is calling toString method on employee class) it should be just `out.write(toString());`, but this doesn't look good hence I recommend to change your method name from `toString()` to `getEmployeeString()` and use `out.write(getEmployeeString());`. I updated the answer. – Yogendra Singh Nov 30 '12 at 22:12
0

When you are executing static void main, you are not in an instance of the class HR but rather in a static method of it. So if you want to use the toString() method you've written, you'd have to do new HR().toString()

You would be better off removing the static keyword from all the fields and creating an instance of the HR object, then opening the file in the Constructor of that object, i.e.

public HR() {
  try {
    this.writeOut = new FileWriter("/Users/zach/Documents/workspace/Dec. 10, 2012/src/ORAGNIZEDPersonnelInfo.txt");
  } catch (IOException e) {
     // handle it, etc.
  }
}

public void processFile() {
  // Do the stuff
}

public static void main(String[] args) {
  HR myHR = new HR().processFile();
  // etc.
}

This would allow you to use the toString() method and allow you to handle the exception the way you want to.

durron597
  • 31,968
  • 17
  • 99
  • 158
0

Others have addresses toString(), so I'll have a go at the FileWriter...

I'd populate writeOut in a method rather than static scope so you can catch the exception and even try a different filename or something:

FileWriter openOutputfile(String name)
{
    boolean done = false;
    FileWriter result = null
    try {
        result = new FileWriter(...);
        done = true;
    }
    catch(IOException ex) {
        // print the stack trace
        // prompt for another filename
        // name = read line
    }
    return result;
}
John3136
  • 28,809
  • 4
  • 51
  • 69