Home > Net >  Using IntelliJ Java Code has minimal errors but won't compile? Not sure how to fix. (Newbie)
Using IntelliJ Java Code has minimal errors but won't compile? Not sure how to fix. (Newbie)

Time:08-04

This is what I am getting after I compile my code but it doesn't come up with any errors when I am actually typing my code. So when I think it is all good to go and good to run it doesn't work. Any help would be appreciated. I'm still really new to 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";
        };
    }
}

Please let me know if adding the code helps. They are all sectioned into parts just like I have them in IntelliJ.

CodePudding user response:

  1. It's not a compilation error but a runtime error (i.e. it was discovered when running the program but the syntax of your code is correct)
  2. The error is on line 158 of FindAShirt.java as indicated in the stacktrace you posted
  3. It seems that you call something like Size.valueOf("somesize") on that line and somesize is not a valid value for the Size enum.
  4. You need to include the code in your question to get the best answers. See: https://stackoverflow.com/help/minimal-reproducible-example

Now that you have posted your code, I suggest you make it more error-proof with this:

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.
}
  • Related