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 4 of 4

Thread: Split a string into individual characters

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Split a string into individual characters

    Hi,

    I want to read in a word from the user and split the string up into each individual characters, and print the individual characters.

    What classes/methods do i need for this ?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Split a string into individual characters

    split the string up into each individual characters,
    Read the API doc for the String class. It has several methods that will do that.
    Java Platform SE 7
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    114
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Split a string into individual characters

    Thanks i have had a go at it.

    The program runs as i wanted but it is throwing an exception at the end of it:

    run:
    Enter a word:
    liverpool
    l
    i
    v
    e
    r
    p
    o
    o
    l
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
    at latesttasks.Task21.main(Task21.java:22)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 3 seconds)
    Here is the code:

    package latesttasks;
     
    // Read in a word as a string and split and print individual characters
    import java.util.Scanner;
     
    public class Task21 {
     
        public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
     
            int stringLength;
            char characters[];
            String input;
     
            System.out.println("Enter a word: ");
            input = scan.nextLine();
            stringLength = input.length();
            characters = input.toCharArray();
     
            for (int i = 0; i <= stringLength; i++) {
                System.out.println(characters[i]);
            }
        }
    }


    --- Update ---

    I have just realised the error,

    i need to do one less on the loop because as it stands the program would be trying to get one index too many.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Split a string into individual characters

    Remember the last valid index for an array is the length-1. The posted code goes past that value when it goes to be =
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Split String and for loop issues. (Beginner problems)
    By AT-LOW in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 7th, 2012, 03:02 PM
  2. Replies: 3
    Last Post: October 26th, 2012, 02:19 PM
  3. Get characters between specific elements in string
    By hacikho in forum Java Theory & Questions
    Replies: 1
    Last Post: November 23rd, 2011, 07:51 PM
  4. Need a non void method to return the first three characters of a string
    By fallout87 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 9th, 2011, 10:00 PM
  5. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM