开发者

How do I send a string to a char array in Java?

开发者 https://www.devze.com 2022-12-10 11:19 出处:网络
String product = Integer.toString(w); char[] original = String.toCharArray(product); This is the code I have so far. The error says that I can\'t use toCharArray on String, but I looked in the docu
String product = Integer.toString(w);

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

This is the code I have so far. The error says that I can't use toCharArray on String, but I looked in the documentation, and it is a listed method, so I'm kind of s开发者_运维问答tuck.


product.toCharArray()

The toCharArray is not a static method, but is a method of a string that already exists, which is why it didn't compile for you.

Here is a longer example:

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


If the original reason was to reverse a number, my sugguestion is

StringBuffer sb = new StringBuffer(Integer.toString(w)); System.out.println(sb.reverse().toString());

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号