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