2012-11-30 64 views
0

我正在使用此程序的這一部分獲得多個erros。它的基本功能是從無組織文件PersonnelInfo.txt中讀取輸入,並將其分類到EmployeeInfo類中的一個數組中(不張貼,因爲我不相信問題在於該類,如果你們想要的話,我會張貼以供參考)。Java寫出程序未處理的異常類型IOException

排序後的信息(如姓名,身份證號,標題,公司等)的程序寫入該到另一個文件,ORGANIZEDPersonnelInfo.txt,將被歸類爲這樣的:

名稱:員工姓名這裏

ID:這裏的員工ID

一個錯誤,我得到的是「靜態的FileWriter WriteOut的......」行,這是「未處理的異常類型爲IOException我把罰球主方法中的IOException d但它沒有做任何事情。

接下來,在「public String toString()」方法中,我收到多個錯誤。當我嘗試使其無效時,它表示返回類型與Object.toString()不兼容。當我把它作爲一個String並返回null時,它希望我使用try/catch,但主要錯誤仍然存​​在。

請幫忙!這是越來越漂亮沮喪......

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; 
} 

回答

0

修正的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(); 
} 

打印內容:

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

但我會寫使用自己的write方法「 writeEmployees(out)「 參見解決方案海報JB Nizet

+0

其寫入文件,但我得到[LEmployeeInfo; @ 18872380 我太會選擇直接寫入文件,但對於這個項目,我們需要使用的方法。 – Zach

+0

Hr hr = new Hr(); out.write(hr.toString()); – AlexWien

+0

但現在它變得有點醜陋。 – AlexWien

0

如果您覆蓋toString()(因爲toSTring()是來自Object的一個方法,並且在您的類中進行了重新定義,所有類都會擴展Object),所以您應該遵守重寫方法的合約,即返回對象的字符串表示形式(而不是將字段寫入緩衝寫入器。用另一個名字命名這個方法。

此外,您正在調用引發檢查異常的方法(out.write()):IOException。所以,要麼你知道如何處理這個異常,你應該抓住它,或者你不知道如何處理它,而該方法應聲明它拋出異常:

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()); 
    } 
} 

您有其他編譯程序中出現錯誤。每一個都帶有一個錯誤信息,一個文件名和一個行號。嘗試理解信息,並修復錯誤。閱讀Java tutorial about exceptions瞭解如何處理它們。

如果在閱讀本教程後仍然無法處理它們,請提出另一個問題並粘貼從編譯器獲得的確切錯誤消息。

0

我會建議您在您的主內移動您的靜態Writer的聲明有兩個原因(一個他們不需要是一個靜態屬性,並且在那裏處理IOException)。從toString獲取字符串,並使用作家寫的文件中的字符串:

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(); 
    }  
} 

如果一定需要讓他們爲靜態變量然後做初始化與異常處理如下靜態塊:

 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(); 
     } 
    } 
+0

非常感謝,這個解決方案讓程序寫入文件,但現在它寫了[LEmployeeInfo; @ 39617189,所以我需要弄清楚發生了什麼。 – Zach

+0

@Zach它從我身邊的簡單錯誤。而不是'out.write(employee.String());'(這是調用toString方法在僱員類)它應該只是'out.write(toString());',但這看起來不好,因此我建議將方法名從'toString()'改爲'getEmployeeString()'並使用'out.write(getEmployeeString());'。我更新了答案。 –

0

當您執行static void main時,您不在HR類的實例中,而是處於靜態方法中。所以,如果你想使用你寫的toString()方法,你必須做new HR().toString()

你會好起來的所有字段刪除static關鍵字和創建HR對象的實例,然後打開文件中該對象的構造函數,即

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. 
} 

這將允許您使用toString()方法,讓你來處理異常,你想要的方式。

0

別人有地址的toString(),所以我必須在一個FileWriter的去...

我的方法填充writeOut而不是靜態的範圍,因此您可以捕獲該異常,甚至嘗試不同的文件名或東西:

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; 
} 
相關問題