2009-11-02 26 views
0
String product = Integer.toString(w); 

char[] original = String.toCharArray(product); 

這是我到目前爲止的代碼。錯誤說我不能在String上使用toCharArray,但我查看了文檔,它是一個列出的方法,所以我有點卡住了。如何向Java中的char數組發送字符串?

+0

它看起來像你試圖用'w'做一些傻事。我可以問爲什麼你想要一個整數到一個char數組? – twolfe18

+0

我需要顛倒一個數字的順序。例如:123456 - > 654321 –

回答

8

product.toCharArray()

的toCharArray是不是一個靜態的方法,但是是一個已經存在的字符串,這就是爲什麼它沒有編譯你的方法。

這裏是一個較長的例子:

public class ToCharArrayString { 
    public static void main(String args[]) { 
    //method converts complete String value to char array type value 
    String str = " einstein relativity concept is still a concept of great discussion"; 
    char heram[] = str.toCharArray(); 
    // complete String str value is been converted in to char array data by 
    // the method 
    System.out.print("Converted value from String to char array is: "); 
    System.out.println(heram); 
    } 
} 
1

如果原始原因是扭轉的數,我的sugguestion是

的StringBuffer SB =新的StringBuffer(Integer.toString(W));的System.out.println(sb.reverse()的toString());

+0

我同意這一點,因爲OP下面的註釋表明這是他試圖實現的,除非我總是在存在時使用更輕量級的'StringBuilder'而不是'StringBuffer'沒有併發擔心。 –

相關問題