title: How do I assign a number to a letter in Java
body: <p>I am working on a rather unique coding situation in Java. The purpose of the program I am trying to write is to take an Amateur Radio call sign, convert the letters in the call sign into numbers as defined by a list or other structure, treat the number as its face integer value, and run these numbers through several mathematical operations to output a unique "User Code" at the end. The length of characters, as well as the number itself, will vary from user to user based on their call sign, which is fine. The biggest obstacle I have encountered is that I do not want the letters to be assigned values in a 1-26 or 0-25 type pattern. I will post my code in a moment to show you. For the moment, the end use of this User Code is unimportant to this example, but suffice it to say that since I will be the only one using this particular code example I am not very concerned with doing validity checks or the like as I will ensure the integrity of the input data manually. With this being said, I do have a working solution which I will post here, but my question is not "it doesn't work" because it does work, my problem is that it is, in my opinion, bloated, and something tells me it could be cut down considerably. Here is the code, and following are some alternatives I considered but rejected:</p>
  
  <pre><code>import java.util.*;
  import java.io.*;
  
  public class UserCode
  {
      public static void main(String[] args)
      {
          Scanner in = new Scanner(System.in);
          int baseNumber = 0;
          int finalNumber;
          String callSign;
          System.out.println("Enter CallSign for Code Generation: ");
          callSign = in.nextLine();
          String s = callSign.toUpperCase();
          for (int i = 0; i &lt; s.length(); i++)
          {
              char c = s.charAt(i);
              if (Character.isDigit(c))
              {
                  int l = Character.getNumericValue(c);
                  baseNumber = baseNumber + l;
              }
              else if (Character.isLetter(c))
              {
                  int letNum = 0;
                  switch (c)
                  {
                      case 'A':
                          letNum = 23;
                          break;
                      case 'B':
                          letNum = 17;
                          break;
                      case 'C':
                          letNum = 5;
                          break;
                      case 'D':
                          letNum = 11;
                          break;
                      case 'E':
                          letNum = 34;
                          break;
                      case 'F':
                          letNum = 18;
                          break;
                      case 'G':
                          letNum = 13;
                          break;
                      case 'H':
                          letNum = 31;
                          break;
                      case 'I':
                          letNum = 27;
                          break;
                      case 'J':
                          letNum = 25;
                          break;
                      case 'K':
                          letNum = 7;
                          break;
                      case 'L':
                          letNum = 25;
                          break;
                      case 'M':
                          letNum = 33;
                          break;
                      case 'N':
                          letNum = 26;
                          break;
                      case 'O':
                          letNum = 28;
                          break;
                      case 'P':
                          letNum = 16;
                          break;
                      case 'Q':
                          letNum = 14;
                          break;
                      case 'R':
                          letNum = 2;
                          break;
                      case 'S':
                          letNum = 4;
                          break;
                      case 'T':
                          letNum = 6;
                          break;
                      case 'U':
                          letNum = 8;
                          break;
                      case 'V':
                          letNum = 10;
                          break;
                      case 'W':
                          letNum = 37;
                          break;
                      case 'X':
                          letNum = 12;
                          break;
                      case 'Y':
                          letNum = 3;
                          break;
                      case 'Z':
                          letNum = 1;
                          break;
                      default:
                          System.out.println("Call Contains a bad character. Try again. \n");
                  }
              baseNumber = baseNumber + letNum;   
              }
          }
      System.out.println("\n");
      String baseStr = Integer.toString(baseNumber);
      System.out.println("The Base number is:  " + baseStr + "\n");
      int sMod = baseNumber%7;
      String sModStr = Integer.toString(sMod);
      System.out.println("The Check Digit is:  " + sModStr + "\n");
      String combine = baseStr + sModStr;
      int nextOp = Integer.parseInt(combine);
      finalNumber = nextOp * nextOp;
      String finalStr = Integer.toString(finalNumber);
      System.out.println("The User Code is:  " + finalStr + "\n");
      }
  }
  </code></pre>
  
  <p>Okay, as I said this code works, but it is long. I had considered a few alternatives, none of which will really work. The first was Enum, but that obviously is outside my parameters as it produces a 1-26 incremented pattern. There are several variations of this using various for{} loops but the result there is the same. I even considered a new HashMap and map.put statements, but that's only marginally shorter and it would seem to me that creating a hashmap would, in the end, actually use more memory than my current solution. As you can see, I did not .split or use .toCharArray() since these created separate entities in memory that I did not need. All the information I needed was already contained in the string itself, except for my chosen numerical values. Finally, I could have created another class file with this assignment code, but the result is the same: the length itself hasn't been changed it's been spread over two files (and actually increased in calling the method).</p>
  
  <p>Having put this out here, can anyone see any way I can shorten this code, particularly in the area of the switch{} block and still retain the same result with the same numerical values? I would be highly interested in any suggestions that can be made in this regard. BTW I didn't mention this but this is not a school assignment, this is a personal project, though my level of Java knowledge is about at that of a beginner taking my first Java class LOL. Thanks</p>