将字母转换为数字 [英] Converting Letters to Numbers
本文介绍了将字母转换为数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个程序将字母转换为数字,以便:
Let's say I have a program which converts letters to numbers such that:
输入:abcd
输出:1234
- 如何有效地将abcd转换为1234
- 我如何提取每一个来自令牌的个人字符
顺便说一句,这不是作业。 (这很有趣)
By the way, this is not homework. (this is for fun)
这是我到目前为止:
public class Test {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("test.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("test.out.txt")));
StringTokenizer st = new StringTokenizer(f.readLine());
int i1 = Integer.parseInt(st.nextToken());
// How can I convert this into integers? (where a = 1, b = 2, and c = 3)
out.println(????);
out.close();
System.exit(0);
}
}
推荐答案
试试这个:
String s = "abcd";
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
sb.append((char)(c - 'a' + 1));
}
// provided your string contains only lower case non-unicode (ASCII) characters.
这篇关于将字母转换为数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文