2015-04-03 47 views
0

有关程序的详细说明:该程序将输入字符串(字母,单词等)并将所有字母转换为已分配给的数字值每个字母字符,然后将结果存储在一个数组中。Java-Android如何将字符串中的字母转换为指定的数字

字母字符数字分配的一个例子如下:

1 = A I J Q Y 
2 = B K R 
3 = C G L S 
4 = D M T 
5 = E H N X 
6 = U V W 
7 = O Z 
8 = F P 

这是使用Java进行Android的我的代码示例:

public class NameCon extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.nameconvert); 

     Button btn = (Button) findViewById(R.id.btnConvert); 
     Button clear = (Button) findViewById(R.id.btnClearName); 
     final EditText et1 = (EditText) findViewById(R.id.edittxtName); 
     final TextView result = (TextView) findViewById(R.id.txtViewSum); 

     result.setText("Input Character Only"); 
     btn.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Map<Character, Integer> map; 
       String str = new String(et1.getText().toString()); 
       str = str.replaceAll("",""); 
       char[]crt = str.trim().toCharArray(); 
       int[] intArray = new int[crt.length]; 

       map = new HashMap<>(26); 
       map.put('a', 1); 
       map.put('i', 1); 
       map.put('j', 1); 
       map.put('q', 1); 
       map.put('y', 1); 
       map.put('b', 2); 
       map.put('k', 2); 
       map.put('r', 2); 
       map.put('c', 3); 
       map.put('g', 3); 
       map.put('l', 3); 
       map.put('s', 3); 
       map.put('d', 4); 
       map.put('m', 4); 
       map.put('t', 4); 
       map.put('e', 5); 
       map.put('h', 5); 
       map.put('n', 5); 
       map.put('x', 5); 
       map.put('u', 6); 
       map.put('v', 6); 
       map.put('w', 6); 
       map.put('o', 7); 
       map.put('z', 6); 
       map.put('f', 8); 
       map.put('p', 8); 

       for(int i = 0; i < crt.length; i ++){ 
        intArray[i] = map.get(crt[i]); 
        result.setText("Values is "+ intArray[i]); 
       } 
      } 
     }); 
    } 
} 

我现在的问题是,我可以没有得到如下所示的结果。然后程序将字母转换为数字。然后每个字母加总。

这是我想说明: 与键盘输入名称=>卡希尔
输出上的TextView秀= 25

非常感谢我的帮助。

回答

1

这应该工作:

public class NameCon extends ActionBarActivity { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.nameconvert); 

    Button btn = (Button) findViewById(R.id.btnConvert); 
    Button clear = (Button) findViewById(R.id.btnClearName); 
    final EditText et1 = (EditText) findViewById(R.id.edittxtName); 
    final TextView result = (TextView) findViewById(R.id.txtViewSum); 
    Map<Character, Integer> map = new HashMap<>(26); 
    map.put('a', 1); 
    map.put('i', 1); 
    map.put('j', 1); 
    map.put('q', 1); 
    map.put('y', 1); 
    map.put('b', 2); 
    map.put('k', 2); 
    map.put('r', 2); 
    map.put('c', 3); 
    map.put('g', 3); 
    map.put('l', 3); 
    map.put('s', 3); 
    map.put('d', 4); 
    map.put('m', 4); 
    map.put('t', 4); 
    map.put('e', 5); 
    map.put('h', 5); 
    map.put('n', 5); 
    map.put('x', 5); 
    map.put('u', 6); 
    map.put('v', 6); 
    map.put('w', 6); 
    map.put('o', 7); 
    map.put('z', 6); 
    map.put('f', 8); 
    map.put('p', 8); 

    result.setText("Input Character Only"); 
    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      String str = et1.getText().toString(); 
      char[]crt = str.trim().toCharArray(); 
      int sum = 0; 

      for(int i = 0; i < crt.length; i ++){ 
       sum += map.get(crt[i]); 
      } 
      result.setText("Values is "+ sum); 
     } 
    });}} 

希望它能帮助!

+0

非常感谢你。^_ ^我可以显示所需的结果。 – Sivilizes 2015-04-03 04:05:25

0

领域声明

private EditText editTextUserInput = null; 
private Button buttonGetTotalNumber = null; 
private HashMap<String, Integer> map = new HashMap<String, Integer>(); 
private char[] charArray = null; 
private ArrayList<Integer> charNumberList = null; 

的onCreate()

map.put("a", 1); 
    map.put("i", 1); 
    map.put("j", 1); 
    map.put("q", 1); 
    map.put("y", 1); 
    map.put("b", 2); 
    map.put("k", 2); 
    map.put("r", 2); 
    map.put("c", 3); 
    map.put("g", 3); 
    map.put("l", 3); 
    map.put("s", 3); 
    map.put("d", 4); 
    map.put("m", 4); 
    map.put("t", 4); 
    map.put("e", 5); 
    map.put("h", 5); 
    map.put("n", 5); 
    map.put("x", 5); 
    map.put("u", 6); 
    map.put("v", 6); 
    map.put("w", 6); 
    map.put("o", 7); 
    map.put("z", 6); 
    map.put("f", 8); 
    map.put("p", 8); 

    editTextUserInput = (EditText)findViewById(R.id.editTextUserInput); 
    buttonGetTotalNumber = (Button)findViewById(R.id.buttonConvertToNumber); 

    buttonGetTotalNumber.setOnClickListener(new OnClickListener() 
    { 

     @Override 
     public void onClick(View v) 
     { 
      // TODO Auto-generated method stub 
      if(editTextUserInput.length()>0) 
      { 
       charNumberList = new ArrayList<Integer>(); 
       charArray = editTextUserInput.getText().toString().trim().toCharArray(); 

       for(int i=0; i < charArray.length;i++) 
       { 
        Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator(); 
        while(iterator.hasNext()) 
        { 
         Map.Entry<String, Integer> dataEntry = iterator.next(); 

         String charData = ""+charArray[i]; 
         if(charData.equalsIgnoreCase(dataEntry.getKey())) 
         { 
          charNumberList.add(dataEntry.getValue()); 
         } 
        } 
       } 
      } 

      Toast.makeText(getApplicationContext(), ""+getTotalOfInput(charNumberList), Toast.LENGTH_LONG).show(); 
     } 
    }); 

方法获取输入的总数

public int getTotalOfInput(ArrayList<Integer> resultNumberList) 
{ 
    int total=0; 
    for(int i=0; i < resultNumberList.size(); i++) 
    { 
     int number = resultNumberList.get(i); 
     total = total + number; 
    } 
    return total; 
} 
相关问题