[英]Using IntelliJ Java Code has minimal errors but won't compile? Not sure how to fix. (Newbie)
這是我在編譯我的代碼后得到的,但是當我實際輸入我的代碼時它沒有出現任何錯誤。 因此,當我認為 go 一切都很好並且運行良好時,它就不起作用了。 任何幫助,將不勝感激。 我對 Java 還是很陌生。
Exception in thread "main" java.lang.IllegalArgumentException: No enum constant Size.34.96
at java.base/java.lang.Enum.valueOf(Enum.java:273)
at Size.valueOf(Size.java:5)
at FindAShirt.loadShirt(FindAShirt.java:158)
at FindAShirt.main(FindAShirt.java:22)
Process finished with exit code 1
public record Shirt (String productName, long productCodeNumber, int price, String brand, Size size, int minPrice, int maxPrice) {
/**
* constructor to create a Shirt object
*
* @param productName the shirt's name
* @param productCodeNumber the shirt's code number - unique 9-digit number
* @param price the shirt's price
* @param brand the shirt's brand
* @param size the shirt's size
* @param minPrice the users min price they will pay for a shirt
* @param maxPrice the users max price they will pay for a shirt
*/
public Shirt {
}
//getters
/**
* @return the shirt's name
*/
public String productName() {
return productName;
}
/**
* @return the shirt's code number - unique 9-digit number
*/
public long productCodeNumber() {
return productCodeNumber;
}
/**
* @return the shirt's price
*/
public int price() {
return price;
}
/**
* @return the shirt's brand
*/
public String brand() {
return brand;
}
/**
* @return the shirt's size via enum
*/
public Size size() {
return size;
}
//create min and max price setters and getters
/**
* @return a 'dream' shirt's min price
*/
public int minPrice() {
return minPrice;
}
/**
* @return a 'dream' shirt's max price
*/
public int maxPrice() {
return maxPrice;
}
/**
* enum representing the 8 t-shirt sizes that the geek store stocks
*/
enum Size {
XS, S, M, L, XL, XXL, XXXL, XXXXL;
public String toString() {
return switch (this) {
case XS -> "Extra Small";
case S -> "Small";
case M -> "Medium";
case L -> "Large";
case XL -> "Extra Large";
case XXL -> "2XL";
case XXXL -> "3XL";
case XXXXL -> "4XL";
};
}
}
如果添加代碼有幫助,請告訴我。 它們都被分成幾部分,就像我在 IntelliJ 中一樣。
Size.valueOf("somesize")
的東西,而somesize
不是Size
枚舉的有效值。既然您已經發布了代碼,我建議您使用以下方法使其更加防錯:
Size size;
try {
size = Size.valueOf(elements[2]);
} catch (IllegalArgumentException e) {
System.out.println(elements[2] + " is not a valid Size");
//and here you may want to exit or use a default value etc.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.