Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Character Array

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Character Array

    How I can convert a string into a character array without using toCharArray???


  2. #2
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Character Array

    What have you tried so far? Show your effort and any errors/problems you are having.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  3. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Re: Character Array

    I have done it with toCharArray(). Now I want to know is there any way to do it without using toCharArray() ???? Is there any please let me know.

  4. #4
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Character Array

    Think of the task logically. A string is an array of characters - so the string
    "Hello" has 4 characters (starting at zero) and ends with a NULL terminating character.

    H[0]
    E[1]
    ...etc

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  5. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Character Array

    import java.util.*;
    public class cArray{
      public static void main(String[]args){
       Scanner input = new Scanner(System.in);
     
        System.out.println("Please Give A String");
        String s1 = input.nextLine();
        char[]data = s1.toCharArray();
      }
    }

    I am converting a String into a Char Array in this way. Now I want to do the same thing without using toCharArray(). so I want to know the alternative way.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Character Array

    Quote Originally Posted by Ada Lovelace View Post
    Think of the task logically. A string is an array of characters - so the string
    "Hello" has 4 characters (starting at zero) and ends with a NULL terminating character.

    H[0]
    E[1]
    ...etc

    Wishes Ada xx
    In java strings are not terminated by a termination character. Thats in C++, C, etc.

    Quote Originally Posted by Mahmudul Hasan View Post
    import java.util.*;
    public class cArray{
      public static void main(String[]args){
       Scanner input = new Scanner(System.in);
     
        System.out.println("Please Give A String");
        String s1 = input.nextLine();
        char[]data = s1.toCharArray();
      }
    }
    Read the string API and try for yourself. There are plenty of ways for doing this.

  7. The Following User Says Thank You to Cornix For This Useful Post:

    Ada Lovelace (July 17th, 2014)

  8. #7
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Character Array

    In java strings are not terminated by a termination character. Thats in C++, C, etc.
    Ah, was not aware - thanks for the info Cornix appreciate it

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

  9. #8
    Junior Member
    Join Date
    Jul 2014
    Posts
    4
    My Mood
    Amazed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Character Array

    Still I cant find another way to do the same thing. Can anyone give me any link. It will be better for me if you give me any link and a hint both. As I am a beginner in java I cant do it
    @Cornix @Ada Lovelace

  10. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Character Array

    Please don't post multiple threads on the same topic. Your other thread was deleted.

    As for your question, we will not write the code or do Internet searching for you. Review the String API for methods that you could use to do what you've described. Try writing the code, and if you run into problems or need help, post the code and ask specific questions about the help you need.

    As you are a beginner, these are basic skills you need to learn and practice.

  11. #10
    Member Ada Lovelace's Avatar
    Join Date
    May 2014
    Location
    South England UK
    Posts
    414
    My Mood
    Angelic
    Thanks
    27
    Thanked 61 Times in 55 Posts

    Default Re: Character Array

    Have you tried to break the string into it's individual characters? As I said previously, a
    string is an array of characters. Think about different ways to do this example: repetition.
    When you have each character of the string, you can then work an algorithm to store the
    character into it's own array.

    Wishes Ada xx
    If to Err is human - then programmers are most human of us all.
    "The Analytical Engine offers a new, a vast, and a powerful language . . .
    for the purposes of mankind
    ."
    Augusta Ada Byron, Lady Lovelace (1851)

Similar Threads

  1. Trying to validate a character user input in an array ?
    By lizzy2 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 20th, 2013, 03:04 PM
  2. Declaring static character array
    By jts0541 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: February 1st, 2012, 09:44 AM
  3. Comparing a character to an array of strings
    By Hashmeer169 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 4th, 2011, 07:55 PM
  4. Can I pass character array to new method?
    By harrydb11 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 21st, 2011, 01:56 PM
  5. Character Array
    By p_dibb in forum Collections and Generics
    Replies: 1
    Last Post: February 16th, 2011, 10:08 AM

Tags for this Thread