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
以下是有效代码。有几件事需要指出:
正如某人提到的,你应该考虑使用一些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