java - 是什么原因导致 "Illegal start of expression"错误?

标签 java compilation

我的方法中的大括号之前的表达式和分号大多是非法的。我真的需要一些帮助,因为我一生都无法弄清楚为什么我会遇到这么多错误

public class stockItem{

    //Declaring Variables
    private double price;
    private  boolean delivery;
    private static int counter = 0;
    // New array with all the catagories stored in it
    private String[] catagoryArray = new String[] {"furniture", "silver", "mirrors", "jewelry", "miscellaneous"};
    private String Catagory;
    private String code;

    //default constructor for stockItem
    public stockItem(){
        price = 0;
        Catagory = catagoryArray[0];
        delivery = false;
        counter++;
        code = "default";
    }

    // Constructor for stockItem made by the user with all nessacey inputs
        public stockItem(String Catagory, double price, boolean delivery){
            for (int i =0; i < catagoryArray.length; i++){
                if (Catagory.equalsIgnoreCase(catagoryArray[i])){
                    this.Catagory = catagoryArray[i];
            } else {
                System.out.println("This is not a valid catagory!");
            }
            // Variables overwrite old variables from default stockItem
            this.price = price;
            this.delivery = delivery;
            counter++;
            code = code();
        }

        // Generates codes for each catagory and each item created
        private String getCode(){
            String furnCode = catagoryArray[0].substring(0,4) + counter;
            String silverCode = catagoryArray[1].substring(0,4) + counter;
            String mirrorsCode = catagoryArray[2].substring(0,4) + counter;
            String jewelry = catagoryArray[3].substring(0,4) + counter;
            String miscCode = catagoryArray[4].substring(0,4) + counter;
        }

        //Methods

        //Returns price of an object
        public double getPrice(){
            return price;
        }

        //Returns true of false for delivery of an object
        public boolean getDelivery(){
                return delivery;
        }

        //Returns the counter value
        public int getCounter(){
                return counter;
        }

        // Returns the catagory of an object
        public String getCatagory(){
                return Catagory;
        }

        // gets code of particular item
        public String getCode(){
                return code;
        }

        // Returns the amount of objects created
        public int objectAmount(){
            return counter;
        }

        //changes price of an object when called
        public void changePrice(double Price){
                this.price = Price;
        }

        // changes delivery option
        public void changeDelivery(boolean changeDelivery){
            this.delivery = changeDelivery;
        }

        // Changes catagory of an object
        public void changeCatagory(String changeCatagory){
            this.Catagory = changeCatagory;
        }

        //Prints a string containing all information about an object
        public String changeToString(){
            String Print = "Catagory:" + Catagory + "Price:" + price + "Amount Created" + counter + "Code" + code;
            return Print;
            }
        }
    }

最佳答案

这看起来不对:

code = code(); // where is code() defined?

您的 getCode() 方法看起来也不正确,因为它有返回值,但什么也不返回。

最后:

//Prints a string containing all information about an object
public String changeToString(){
    String Print = "Catagory:" + Catagory + "Price:" + price + "Amount Created" + counter + "Code" + code;
    return Print;
    } // this should be removed
}

关于java - 是什么原因导致 "Illegal start of expression"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27524894/

相关文章:

c - 左值需要作为一元 '&' 操作数

python - 编译python模块扩展时如何静态链接库

java - 当 @RequestMapping 正常工作时,为什么 @GetMapping 不起作用?

java - RecyclerAdapter getItemCount 崩溃

java - 如何返回由 Jersey Rest 中的客户端执行的 javascript 代码?

c++如何在编译时创建我自己的警告

c - 目标代码,C语言中的链接时间

java - 为什么即使在 volatile 修复之后仍然不鼓励双重检查锁定?

java - 如何在 Java 中为 AWS Cognito 用户池中的登录用户以编程方式启用或禁用 MFA?

c++ - 使用 ncurses 时未定义对 `stdscr' 的引用