簡體   English   中英

Java的 <identifier> 期望與writeObject()

[英]Java <identifier> expected with writeObject()

對於類賦值,我需要將對象寫入文件。 我們的教授給了我們一段代碼來完成這個,但顯然這是錯誤的,因為我收到了一個錯誤。 這是我的代碼。

class InvMaintenance {

    //create an OutputStream to write data to a file
    FileOutputStream fos = new FileOutputStream(inven.dat);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ObjectOutputStream oos = new ObjectOutputStream(bos);

    final long MAX_SIZE = 100; //constant for array length
    Inventory cInventory = new Inventory(MAX_SIZE); //instantiate Inventory object
    oos.writeObject(cInventory); //write initial Inventory to file  
    public static void main(String[] args) {
        //Output options
        /*  Inventory Maintenance
            1) Add Item
            2) Remove Item
            3) Sell Item
            4) Receive Item
            5) Display Inventory
            6) Quit
            Please Select NUMBER: */

        //switch on options
        //call appropriate method

        oos.writeObject(cInventory);
        oos.close();
    }
}

我的錯誤發生在oos.writeObject(cInventory);

Item.java:150: <identifier> expected
    oos.writeObject(cInventory);            //write initial Inventory to file   
                   ^
Item.java:150: <identifier> expected
    oos.writeObject(cInventory);            //write initial Inventory to file   
                              ^
2 errors

是的,出於某種原因,它說這是兩個完全相同的錯誤。

任何幫助調試將不勝感激。 怎么了?

必須在主方法中

//create an OutputStream to write data to a file
FileOutputStream fos = new FileOutputStream(inven.dat);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);

Inventory cInventory = new Inventory(MAX_SIZE); //instantiate Inventory object

改變代碼:

class InvMaintenance {

    final static long MAX_SIZE = 100;          //constant for array length

    public static void main(String[] args)
    {
        //Output options
            /*  Inventory Maintenance
                1) Add Item
                2) Remove Item
                3) Sell Item
                4) Receive Item
                5) Display Inventory
                6) Quit
                Please Select NUMBER: */

        //switch on options
        //call appropriate method

        //create an OutputStream to write data to a file
        FileOutputStream fos = new FileOutputStream(inven.dat);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        ObjectOutputStream oos = new ObjectOutputStream(bos);

        Inventory cInventory = new Inventory(MAX_SIZE); //instantiate Inventory object

        oos.writeObject(cInventory);
        oos.close();
    }   

}

提示:將常量更改為最終的靜態長度。使用靜態值,在編譯時復制值

提示2:照顧例外..

編譯器抱怨以下行:

oos.writeObject(cInventory);            //write initial Inventory to file   

除了所有方法之外,你不能擁有這樣的獨立代碼。 將其移動到main()

它之前的變量聲明( FileOutputStream fos ... )也應該移動到main() 他們會像現在一樣編譯,但以后會引起你的問題。

writeObject方法的javadoc中所述,它會拋出IOException 因此,您應該通過在try-catch塊中設置語句來捕獲異常,或者讓main方法拋出異常

此外,您嘗試從靜態上下文訪問非靜態字段,這也不起作用。

最后,你的語句是oos.writeObject(cInventory); 在你的主要方法之外也不會有效,因為你不能在你的班級里找到陳述。

可執行代碼(如方法調用)必須位於方法或可能是靜態塊內,但在您的情況下,它明顯屬於main方法。 將代碼移到那里就可以了。

此外, main方法是程序入口點。 除非控制流有一些從main方法內部到你想要執行的代碼的方式,否則什么都不會發生。

你在哪里定義以下代碼?

//create an OutputStream to write data to a file
FileOutputStream fos = new FileOutputStream(inven.dat);
BufferedOutputStream bos = new BufferedOutputStream(fos);
ObjectOutputStream oos = new ObjectOutputStream(bos);

final long MAX_SIZE = 100;          //constant for array length
Inventory cInventory = new Inventory(MAX_SIZE); //instantiate Inventory object

oos.writeObject(cInventory);            //write initial Inventory to file   

你有你的班級,你有一個“主要方法”,但代碼在哪里? 附加的事情:它是什么“invent.dat”

順便說一句,如果你有編譯錯誤,你無法真正調試你的代碼,因為你的代碼應該是編譯器之前的...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM