Java-Android如何将字符串中的字母转换为指定的数字 [英] Java-Android How to convert letters in a string to specified number
问题描述
关于程序的详细说明:该程序采用输入字符串(字母,单词等)并将所有字母转换为为每个字母字符分配的数值,然后将结果存储在数组中。 / p>
下面显示了字母字符数字赋值的示例:
1 = AIJQY
2 = BKR
3 = CGLS
4 = DMT
5 = EHNX
6 = UVW
7 = OZ
8 = FP
这是我使用Java for Android的代码示例:
公共类NameCon扩展ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.nameconvert);
Button btn =(Button)findViewById(R.id.btnConvert);
按钮清除=(按钮)findViewById(R.id.btnClearName);
final EditText et1 =(EditText)findViewById(R.id.edittxtName);
final TextView result =(TextView)findViewById(R.id.txtViewSum);
result.setText(仅限输入字符);
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]);
}
}
});
}
}
我现在的问题是我无法得到结果如下所示的例子。然后程序将字母转换为数字。然后每个字母总和。
这是我要显示的:
带键盘的输入名称=> Tim Cahill
输出showview = 25
非常感谢帮助我。
这应该有效:
公共类NameCon扩展ActionBarActivity {
@Override
protected void onCreate(Bundle) savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.nameconvert);
Button btn =(Button)findViewById(R.id.btnConvert);
按钮清除=(按钮)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(仅限输入字符);
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);
}
});}}
希望有所帮助!
Detail explanation about the program: This program takes an input string (letter, word etc..) and converts all the letters into a numerical value that has been assigned for each alphabetic character, then stores the result in an array.
An example of the numerical assignment for alphabetic characters is shown below:
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
This is an example of my code using Java for 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]);
}
}
});
}
}
My problem now is that I can not get the result as the examples shown below. Then the program to convert the letters to numbers. Then each of the letters to sum.
This is I want to show:
Input Name with keyboard => Tim Cahill
Output show on textview = 25
Many thanks for helping me.
This should work :
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);
}
});}}
Hope it helps !
这篇关于Java-Android如何将字符串中的字母转换为指定的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!