1

Whenever I put .toLowerCase or .toUpperCase it doesn't work for me. It shows me the error constant string expression required. I was wondering if anyone could help me figure out how to fix this. Here's some code to help you out.

static final String SCISSORS = "Scissors".toUpperCase();

switch (choice) {
    case SCISSORS:
        System.out.println("I choose scissors");
        break;
    case PAPER:
        System.out.println("I choose paper");
        break;
    case ROCK:
        System.out.println("I chose rock");
        break;
}
enzo
  • 9,861
  • 3
  • 15
  • 38
A.C.
  • 13
  • 1
  • 4
  • This question may be of interest to you: https://stackoverflow.com/questions/3827393/java-switch-statement-constant-expression-required-but-it-is-constant/3827488 – Schuyler Sloane Jun 12 '21 at 18:13
  • And indeed, there's little reason to use .toUpperCase in that context,, since it's easy enough to type SCISSORS direcrly, – iggy Jun 12 '21 at 20:06

2 Answers2

0
  • You must surround each value with " characters to use switch with Strings

  • The choice and SCISSORS fields are not related. You must fix that by changing the declaration. This is a possible solution:

      final String choice = "Scissors".toUpperCase(); 
      switch (choice) { 
          case "SCISSORS": System.out.println("I choose scissors"); break; 
          case "PAPER": System.out.println("I choose paper"); break; 
          case "ROCK": System.out.println("I chose rock"); break;
      }
    
  • Necessity to use quoted strings - not exactly; it is sufficient that the values in the case clauses are all considered to be constant by the language rules. Though not apparent in the OP's small example, it's often structurally advantageous to declare name for constant strings "once and for all". – iggy Jun 12 '21 at 20:09
  • There are no structural advantages requirements or issues expressed from the OP – Victor Polo De Gyves Montero Jun 12 '21 at 20:46
  • The OP is apparently a beginning Java programmer, one wouldn't expect such. But since he's a beginning programmer, we should not say "you must" when that is not in fact mandatory. – iggy Jun 12 '21 at 21:51
  • To decide if it is mandatory or not depends on the OP's needs, neither the two of us can judge that. If the OP wants to keep its solution as close as possible to his code, then it is mandatory. Only the OP knows his needs, not the language. – Victor Polo De Gyves Montero Jun 13 '21 at 02:04
  • When I talk about "mandatory" I refer to the Java language as it is actually defined.by the actual language specification. – iggy Jun 13 '21 at 12:51
  • "You must" is not defined in the language spec. No way to conclude that "you must" it is "mandatory" as in the spec on that perspective, too. It also does not makes sense on the OP's question to refer to the language specification. – Victor Polo De Gyves Montero Jun 14 '21 at 03:21
  • Enough of this merry banter; my (working) answer shows the technique I wish to demonstrate. The OP can decide which he prefers. – iggy Jun 14 '21 at 12:21
  • 1
    Thanks for your help Victor, this really helped me out. – A.C. Jun 14 '21 at 18:06
0

There's little point in using .toUpperCase on a string that literally appears in your source code, when it's easy enough to write the upper case form yourself.

The trick is to make the various fixed values into compile-time constants, as I have done in the following quick example. Then you can use the values in 'case' clauses in a switch statement.

I intentionally used single-character names for the constants to make it clear whether we were talking about the name or the value.

It is of course necessary (or at least user-friendly) to convert the user input to upper case as well, prior to the comparison.

I'm a lazy typist so I did not bother to declare a 'choice' variable, I just read the required value directly in the switch statement. I didn't prompt either. Don't do this at home.

import java.util.Scanner;
public class S {
    final static String S = "SCISSORS";
    final static String R = "ROCK";
    final static String P = "PAPER";
    public static void main(String... args) {
        switch (new Scanner(System.in).next().toUpperCase()) {
        case S: System.out.println("Scissors"); break;
        case R: System.out.println("Rock"); break;
        case P: System.out.println("Paper"); break;
        }
    }
}
iggy
  • 1,328
  • 4
  • 3