Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是什么原因引起的;“表达式开头非法”;错误?_Java_Compilation - Fatal编程技术网

Java 是什么原因引起的;“表达式开头非法”;错误?

Java 是什么原因引起的;“表达式开头非法”;错误?,java,compilation,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 i

我的方法中的大括号之前的表达式和分号通常是非法的。我真的需要一些帮助,因为我一辈子都不明白为什么我会犯这么多错误

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;
            }
        }
    }
公共类库存项目{
//声明变量
私人双价;
私人布尔传递;
专用静态整数计数器=0;
//新阵列中存储了所有类别
私有字符串[]分类数组=新字符串[]{“家具”、“银”、“镜子”、“珠宝”、“杂项”};
私有字符串分类;
私有字符串码;
//stockItem的默认构造函数
公共物品(){
价格=0;
分类法=分类法[0];
交付=错误;
计数器++;
code=“默认”;
}
//由用户使用所有nessacey输入创建的stockItem的构造函数
公共库存项目(字符串分类、双倍价格、布尔交付){
for(int i=0;i
这看起来不对:

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
}

除了Eran指出的东西外,stockItems中还有一个无与伦比的大括号。for循环缺少最后一个大括号

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!");
        }

    } // This one.

    // Variables overwrite old variables from default stockItem
    // ... code ...
}
publicstockItem(字符串分类、双倍价格、布尔交付){
for(int i=0;i
以下是有效代码。有几件事需要指出:

  • 您应该命名一个类StockItem,而不是StockItem。阅读Java命名约定
  • 您有tvo getCode()方法。不能那样做。我将其中一个重命名为getCode1()。选择一个你喜欢的名字。这个方法应该返回一个字符串
  • 构造函数中缺少结束括号。我加了一些,但不确定位置是否正确
  • 正如某人提到的,你应该考虑使用一些IDE。

    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!");
                }
            } // not sure if the closing bracket should be here
            // Variables overwrite old variables from default StockItem
            this.price = price;
            this.delivery = delivery;
            counter++;
            code = getCode();
        }
    
        // Generates codes for each catagory and each item created
        private String getCode1() {
            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;
            return ""; // this method should return a string!
        }
    
        //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;
        }
    
    }
    
    公共类库存项目{
    //声明变量
    私人双价;
    私人布尔传递;
    专用静态整数计数器=0;
    //新阵列中存储了所有类别
    私有字符串[]分类数组=新字符串[]{“家具”、“银”、“镜子”、“珠宝”、“杂项”};
    私有字符串分类;
    私有字符串码;
    //StockItem的默认构造函数
    公共物品(){
    价格=0;
    分类法=分类法[0];
    交付=错误;
    计数器++;
    code=“默认”;
    }
    //由用户使用所有nessacey输入创建的StockItem的构造函数
    公共库存项目(字符串分类、双倍价格、布尔交付){
    for(int i=0;i