Results 1 to 7 of 7
  1. #1
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default Creating Array of Characters From a String [Solved]

    As the titled says, I am looking to try to make an array of the characters from a string. How should I go about doing this.
    Last edited by ngc0202; 07-30-2009 at 03:26 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Look in the String API and you should find a method that does this for you.

  3. #3
    webbusiness23 is offline Member
    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

    Default

    You need to know the length of your String.
    The size of your char[] is the same as your String's length.


    Here's the code:


    PHP Code:
    public class StringToArray {
    	public static void main(String args[]){
    		String text = "someText";
    		
    		int l = text.length();
    		char[] v = new char[l];
    		
    		for (int i=0; i<l; i++){
    			v[i] = text.charAt(i);
    			System.out.println(v[i]);
    		}
    		
    	}
    }

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by webbusiness23 View Post
    You need to know the length of your String.
    The size of your char[] is the same as your String's length.
    Here's the code:
    I disagree with this and go with my original recommendation. If you look in the String API, you'll find the toCharArray() method which creates the array in one fell swoop. So if we are going to be spoon-feeding answers here (much better to learn to look into the API in my opinion), I think a simpler and more straight-forward solution would be:
    Java Code:
    public class Fu1 {
      public static void main(String args[]) {
        String text = "someText";
    
        // isn't this much simpler?
        char[] myCharArray = text.toCharArray();
    
      }
    }

  5. #5
    ngc0202 is offline Member
    Join Date
    Jul 2009
    Posts
    32
    Rep Power
    0

    Default

    Thanks. With a little editing, the code worked perfectly!

    EDIT: I was talking to webbusiness23, I never saw your post Fubarable as I didn't refresh since arriving at webbusiness' post.
    Last edited by ngc0202; 07-30-2009 at 01:10 AM.

  6. #6
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default Some comments...

    @ngc0202: Although the solution that webbusiness23 proposed is a good exercise, it would be better and more productive to use the String method as Fubarable suggested. Here's the link to the Javsa String APIs:

    String (Java Platform SE 6)

    @webbusiness23: Although you were were trying to help the OP, it's not a good idea to post complete working code... too much temptation to cut, paste & forget. It is a much better to give pseudo code, links, snippets, etc. This way the OP develops code and learns.

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  7. #7
    webbusiness23 is offline Member
    Join Date
    Jul 2009
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by CJSLMAN View Post

    @webbusiness23: Although you were were trying to help the OP, it's not a good idea to post complete working code... too much temptation to cut, paste & forget. It is a much better to give pseudo code, links, snippets, etc. This way the OP develops code and learns.

    Luck,
    CJSL
    Ok, i will keep that in mind.

Similar Threads

  1. characters from a string into an integer
    By 2potatocakes in forum New To Java
    Replies: 7
    Last Post: 05-08-2012, 01:31 PM
  2. Shifting characters in array
    By Mayur in forum New To Java
    Replies: 2
    Last Post: 04-24-2009, 11:19 PM
  3. deleting characters from a String
    By Hayzam in forum New To Java
    Replies: 4
    Last Post: 08-29-2008, 01:14 PM
  4. how to get the characters one by one from a String?
    By Somitesh Chakraborty in forum New To Java
    Replies: 3
    Last Post: 08-20-2008, 09:56 PM
  5. Getting all characters in a String
    By Alayna in forum New To Java
    Replies: 2
    Last Post: 05-20-2007, 12:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •