Switch Statement

Scanner scanner = new Scanner(System.in);
        System.out.print("Level: ");
        int level = scanner.nextInt();

        if (level <= 4)
            System.out.print("You are an Associate".toLowerCase(Locale.ROOT));
        else if (level > 4 && level <= 10 )
            System.out.print("You are a Manager");
        else
            System.out.print(" You are Jeff Bezos".toUpperCase(Locale.ROOT));

How can I turn these if statements into Switch statements? I keep getting errors because my condition is not an int. i’ve tried parsing but i’m not sure i was doing it right

In Java you can’t because the expression after the switch needs to be of an integer, character, string or enum type and the expression after case needs to be constant.

Given that your level has a minimum value you could enumerate the values that fall into each category:

switch (level)
{
   case 1:
   case 2:
   case 3:
   case 4:
     System.out.print("You are an Associate".toLowerCase(Locale.ROOT));
      break;
[...]
   default:
      System.out.print(" You are Jeff Bezos".toUpperCase(Locale.ROOT));
      break;
}

or with switch expressions

switch (level)
{
   case 1, 2, 3, 4 => System.out.print("You are an Associate".toLowerCase(Locale.ROOT));
   case 5, 6, 7, 8, 9, 10 => System.out.print("You are a Manager".toLowerCase(Locale.ROOT));
   default => System.out.print(" You are Jeff Bezos".toUpperCase(Locale.ROOT));
}

but of course that’s not very maintainable.

Thank-you; This was very instructive, I appreciate it !