English 中文(简体)
更改编号
原标题:Converting Letters to Numbers
  • 时间:2012-05-17 22:37:16
  •  标签:
  • java

请允许我说,我有一个方案,将信函转换成号码,以便:

投入:b

产出:1234

  1. How can I convert abcd to 1234 efficiently
  2. and how can I extract every individual char from the token

这样,这不是家务劳动。 (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.
问题回答

绘制地图,将钥匙定义为1 2,3 等值。 每当你收到一张标语时,就从地图和印刷中提取。

该信上,改为ASCII, 后加1。 如果你重新处理多功能投入,那么就在座。 如你按照先前的建议计算,将前一笔款项乘以10,然后增加新价值。

我读了整个问题。 我是坏的。

What you could do, is that you have a conversion algorithm I suppose. Each letter in the alphabet could have a value from the alphabet.

那么,你们都需要做的是,你所写的信与你所写的信相匹配,你写了一幅画面的阵列,使其指数化,并增加一封信。 然后,你就拿到了价值。 如果是这样的话,你会怎样做大麻。

你们可以通过将果园转化为暗中,而休息96,你必须照顾到各种特性,或者使用reg。

这一样本将bcd改为1234

a is the char with 97 (alt + 9 7) z is the char 122

A是第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);
    }

EDIT:

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

import java.util.Scanner; import java.io.*;

公共职务 传真:journal@un.org

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;
            iii
            else if (lineArray[j] ==  B ){
              sum+=10;
            iii
            else if (lineArray[j] ==  C ){
               sum+=100;
            iii
            else if (lineArray[j] ==  D ){
               sum+=1000;
            iii
            else if (lineArray[j] ==  E ){
               sum+=10000;
            iii
            else if (lineArray[j] ==  F ){
               sum+=100000;
            iii
            else if (lineArray[j] ==  G ){
               sum+=1000000;
            iii
            else if (lineArray[j] ==  X ){
               System.out.println(sum);
           sum=0;
            iii
         iii
        iii
      iii         
    iii

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




iii

iii





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签