i have a project that i’m working on in my class which asks you to use switch case to use the user’s waist measurement to get their pant size.
however, when i start the case in the switch case it gives me the error on line 13.
public class MySizes {
public static void main(String[] args) throws IOException {
int waistMeasurement; //creates variable
Scanner in = new Scanner(System.in); //allows user input
System.out.println("Hello, customer. In order to find pants in your size I need to know your waist measurement. \nWhat is your waist measurement(enter in centimetres)?");
waistMeasurement = in.nextInt(); //reads the user input and stores it in the waistMeasurement vairiable
System.out.println("Your waist measurement is " + waistMeasurement + "cm. I will look at our sizing chart to see what size you'll wear.");
switch (waistMeasurement) {
case (<66):
System.out.println("Your pants size is small.");
break;
}
}
}
I tried googling around and searching on this website and read that I might be missing a curly bracket but I triple-checked and it doesn’t seem like I am.. The other thing I could think of is maybe I can’t figure out how to compare the variable to the number 66 the right way.. but even when I tried to it can’t be converted to a Boolean, so I don’t know how to do that.
By the way, I’m not very knowledgeable on this programming language, so if it’s possible (depending on what I did wrong it may not be) I would prefer a simpler explanation to what I could do to fix it.
I was expecting the switch case to not have an error, However it had an error and I can’t understand what may be causing it.
>Solution :
Your case
cannot contain a condition this way. You likely want to use a conditional.
switch (waistMeasurement) { case (<66): System.out.println("Your pants size is small."); break; }
Becomes:
if (waistMeasurement < 66) {
System.out.println("Your pants size is small.");
}
As a sidenote, break
is used in switches to prevent fallthrough, so that only one case is executed and not any following cases. There are times fallthrough may be the desired behavior.
Because you only had one case in your switch, break
would be extraneous.