0

The variable "varOptions" is attained via scanner. Basically, I want: [A, B, C, D, E, F] to become: [0, 1, 2, 3, 4, 5]. It is also very important that the code remain non-case sensitive. Currently, this code is rather bulky. I'm wondering if there is a way to make this more efficient?

    if ("A".equalsIgnoreCase(varOptions)) {
        int varOpt = 0;
    } else if ("B".equalsIgnoreCase(varOptions)) {
        int varOpt = 1;
    } else if ("C".equalsIgnoreCase(varOptions)) {
        int varOpt = 2;
    } else if ("D".equalsIgnoreCase(varOptions)) {
        int varOpt = 3;
    } else if ("E".equalsIgnoreCase(varOptions)) {
        int varOpt = 4;
    } else if ("F".equalsIgnoreCase(varOptions)) {
        int varOpt = 5;
    }
Jonathan Mousley
  • 143
  • 1
  • 1
  • 5

5 Answers5

0

Convert the varOptions variable to a character if it is not already (as opposed to a string). Once it's a character, you can cast it to an integer, which will convert the character to its ASCII code. The 'A' character has an ASCII code of 65. B is 66. C is 67, etc. etc.

To remain case insensitive, convert the character to uppercase before converting to an integer.

Therefore, after casting it to an integer, you just subtract 65, and you will have a number between 0 and 5:

int varOpt = (int)Character.toUppercase(varOptions);
varOpt -= 65;

The code above assumed varOptions is already of type char. Note that the case to an int is not required, but improved readability. One could just as easily do:

int varOpt = Character.toUppercase(varOptions);

Full code example, assuming varOptions is a string:

char varOptChar = varOptions.charAt(0); // grab first character
int varOpt = (int)Character.toUppercase(varOptChar);
varOpt = varOpt - 65;

varOpt should now be between 0 and 5 for your example. To understand this you need to understand that characters have decimal representations in a computer. The computer only sees the number 65, but knows that this number indicates an uppercase A. 65 is called an ASCII code; you can find a table of ASCII codes here: http://www.asciitable.com/

Matt Coats
  • 332
  • 1
  • 5
0
int varOpt = Character.toUppercase( varOptions.charAt(0) ) - 'A'; // assuming varOptions is String

or

int varOpt = Character.toUppercase( varOptions ) - 'A'; // assuming varOptions is char

You can also use switch on String:

int varOpt;
switch( varOptions.toUpperCase() ) {
    case "A":
        varOpt = 0; break;
    case "B":
        varOpt = 1; break;
    // etc
}

or use a Map, if you intend to do something else than simple char-to-int mapping over a continuous range.

0

How about:

int varOpt = " ABCDEF".indexOf(varOptions.toUpperCase());
Bohemian
  • 412,405
  • 93
  • 575
  • 722
-1

Its not that pretty, but you could do something like this

Map<String, Integer> map = new HashMap<String, Integer>()
{{
   put("A", 1);
   put("B", 2);
   put("C", 3);
   put("D", 4);
   put("E", 5);
   put("F", 6);
}};

// varOpt will be null if it is not in the map
int varOpt = map.get(varOptions.toUpperCase());
user1875195
  • 968
  • 4
  • 8
  • 22
  • 1
    I have never seen that double curly bracket notation. Does that create an anonymous subclass of HashMap which has an additional static initializer? – Teto Jul 13 '16 at 00:12
  • @Teto yup. it's also generally discouraged for a couple of reasons - https://blog.jooq.org/2014/12/08/dont-be-clever-the-double-curly-braces-anti-pattern/ https://blog.nishtahir.com/2015/09/27/why-you-shouldnt-use-the-double-brace-initializer/ http://stackoverflow.com/questions/1958636/what-is-double-brace-initialization-in-java http://stackoverflow.com/questions/924285/efficiency-of-java-double-brace-initialization - *"every time you use DBI, a kitten get killed"* –  Jul 13 '16 at 10:13
-1

An alternative is to parse the letter as an hexadecimal number and subtract 11:

int varOpt = Integer.parseInt(varOptions, 16) - 0xA;
assylias
  • 321,522
  • 82
  • 660
  • 783