2012-05-17 158 views
1

比方說,我有一個計劃,轉換字母與數字使得:轉換字母與數字

輸入:ABCD

輸出:1234

  1. 我怎麼能轉換ABCD至1234高效
  2. 以及如何從令牌中提取每個單獨的字符

B順便說一句,這不是功課。 (這是爲了好玩)

這是我到目前爲止有:

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

大寫字母,將其轉換爲ASCII,減去 'A',再加入1 –

+0

你的問題需要更多的規範。例如,'wxyz'應該轉化爲什麼? –

回答

3

試試這個:

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. 
+7

你給了他魚! –

+4

不只是魚,他給了他在盤子裏煮熟的魚。 :) –

+1

Booo沒有免費的魚!我們要求混淆! – slezica

0

DERP,我讀了整個問題是錯誤的。我的錯。

你能做什麼,是你有一個我認爲的轉換算法。 字母表中的每個字母都可以包含字母表中的值。

然後你所需要做的就是將你找到的字母與你的一個字母數組相匹配,得到它的索引,然後給它加1。然後你得到了價值。如果這就是你想玩的方式。

1

有每當你收到一個令牌採取從地圖和打印帶有按鍵爲A,B,C等具有值1,2,3等定義的地圖。

+1

+1這種技術允許範圍廣泛的惡作劇。 –

+0

謝謝@TonyEnnis – raddykrish

1

大寫字母,將其轉換爲ASCII,減去'A',然後加1.如果處理多字符輸入,則遍歷字符串。按照前面的建議計算時,將先前的總和乘以10,然後添加新的值。

0

我想這是你正在尋找的方法:StringcharAt

的樣本代碼,將你所追求的:

String a="abcd"; 
for (int i = 0; i < a.length(); ++i) { 
    System.out.print(a.charAt(i)-'a'+1); 
} 
0

您可以嘗試轉換字符爲int休息96,你要照顧超出範圍的字符或使用正則表達式..

此示例轉換ABCD至1234

與97(ALT + 9 7) z是炭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); 
    } 
0

import java.util.Scanner; import 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"); 
     } 




} 

}

+0

歡迎來到Stack Overflow!請解釋你的代碼的作用以及爲什麼它能解決問題。只包含代碼的答案(即使它正在工作)通常不會幫助OP瞭解他們的問題。 – SuperBiasedMan