0
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 stuck.

2
  • it looks like you are trying to do something silly with w. can i ask why you want to get an integer to a char array? Commented Nov 2, 2009 at 1:44
  • I need to reverse the order of a number. Example: 123456 -> 654321 Commented Nov 2, 2009 at 1:47

2 Answers 2

8

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);
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

I agree with this, given that the comment below the OP says that this is what he was trying to achieve, except I'd always use the more lightweight StringBuilder instead of StringBuffer when there is no concurrency to worry about.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.