1

假设我有一个将字母转换为数字的程序,这样:

输入:abcd

输出:1234

  1. 如何有效地将 abcd 转换为 1234
  2. 以及如何从令牌中提取每个单独的字符

顺便说一句,这不是家庭作业。(这是为了好玩)

这是我到目前为止所拥有的:

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);                              

    }

}
4

7 回答 7

5

尝试这个:

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.
于 2012-05-17T22:40:36.987 回答
1

大写字母,将其转换为 ASCII,减去“A”,然后加 1。如果您正在处理多字符输入,则遍历字符串。当您根据之前的建议进行计算时,将之前的总和乘以 10,然后添加新值。

于 2012-05-17T22:41:01.680 回答
1

使用键定义为 a、b、c 等的地图,其值为 1、2、3 等。每当您收到令牌时,请从地图中获取并打印。

于 2012-05-17T22:40:54.453 回答
0

我想这是您正在寻找的方法:String's charAt

一个示例代码可以满足您的需求:

String a="abcd";
for (int i = 0; i < a.length(); ++i) {
   System.out.print(a.charAt(i)-'a'+1);
}
于 2012-05-17T22:42:41.563 回答
0

您可以尝试将 char 转换为 int 并休息 96,您必须处理超出范围的字符或使用正则表达式 ..

此示例将 abcd 转换为 1234

a 是 97 的字符 (alt + 9 7) z 是 122 的字符

A 是 char 65,因此您可以按范围处理或将单词转为小写

public static void main(String args[]){
        String abc = "abcd";
        String txt = "";

        for(int i=0; i<abc.length(); i++){
            txt+= ( (int)abc.charAt(i) - 96 );
        }

        System.out.println(txt);
    }

编辑:

public static void main(String args[]){
        String abc = "AbCd";
        String txt = "";
        abc = abc.toLowerCase();

        for(int i=0; i<abc.length(); i++){
            int letter = (int)abc.charAt(i);

            if(letter<97 || letter > 122)//in range
            {
                txt += ( (int)abc.charAt(i) - 96 );
            }

        }

        System.out.println(txt);
    }
于 2012-05-17T22:42:43.297 回答
0

Derp,我读错了整个问题。我的错。

你可以做的是,我想你有一个转换算法。字母表中的每个字母都可以具有字母表中的值。

然后你需要做的就是匹配你找到的字母,与你的一个字母一个字符串数组,得到它的索引,然后加1。然后你得到了价值。如果这就是你想玩的方式。

于 2012-05-17T22:39:36.080 回答
0

导入 java.util.Scanner;导入java.io.*;

公共类 LetteredNumerationSystem{

public static void main(String args[]){

File file = new File ("lettered.in");

    try{

    Scanner input = new Scanner(file);

    int dataCollect = input.nextInt();
    int sum=0;
    String lineInput ="";


      for(int i=0;i<=dataCollect;i++){
      while (input.hasNext()){
      lineInput=input.next();       
      char lineArray[] = lineInput.toCharArray();
         for(int j=0;j<lineArray.length;j++){
            if (lineArray[j] == 'A'){
              sum+=1;
            }
            else if (lineArray[j] == 'B'){
              sum+=10;
            }
            else if (lineArray[j] == 'C'){
               sum+=100;
            }
            else if (lineArray[j] == 'D'){
               sum+=1000;
            }
            else if (lineArray[j] == 'E'){
               sum+=10000;
            }
            else if (lineArray[j] == 'F'){
               sum+=100000;
            }
            else if (lineArray[j] == 'G'){
               sum+=1000000;
            }
            else if (lineArray[j] == 'X'){
               System.out.println(sum);
           sum=0;
            }
         }
        }
      }         
    }

    catch(FileNotFoundException e){
        System.out.println("ERROR");
        }   




}

}

于 2015-07-08T10:33:20.533 回答