Results 1 to 12 of 12
  1. #1
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default Splitting single string into array elements

    Hello. I'm trying to accept an input and that input delimited by spaces in which those individual strings get inputted into an array. So if i was to enter

    single string "This is a test"
    1 2 3 4 would be the array elements.


    all i need to know is how to convert that single string into individual strings so i can put them into an array.

    Thanks people

  2. #2
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    Essentially:

    Java Code:
    String[] elements = str.split(" +");
    Read about the Java regular expressions API to find out more.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  4. #4
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default

    Is there any way to use that split method for a multi dimension array?

    Cause what i'm trying to achieve is:

    User enters their name (firstname, middlename, lastname);
    Then they get stored seperate into an array.
    a count, so each time a person enters a new name it doesnt get over written by the previous one.

    i.e.

    [0] [1] [2]
    [0] [firstname] [middle name] [last name]
    [1] [firstname] [middle name] [last name]
    [2] [firstname] [middle name] [last name]

    i'm not too sure how to implement this using the split method when it only accepts a single array.

    please help
    thanks

  5. #5
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Neil is right about split(). It give back exactly what you want from your input.

    Beyond that, you need to be a bit more sophisticated with your information. I suggest creating a simple Name class that has three properties, first, middle, and last. As you create each name, store it in an ArrayList, not an array.

    Last, your logic has a little problem. Not everyone has a first, middle, last name, and not everyone puts their "first" name first. In the most common situation, John Smith, who has no middle name, will end up with first="John", middle="Smith", last=?.

    You are better off to prompt for given name (first), middle name, surname (last). If you really want to be sensitive, add a check box for "Show surname first".

  6. #6
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    7

    Default

    I'm guessing this is just an exercise, not a production system. Strictly, people can also have spaces in their surname...

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by phil128 View Post
    Is there any way to use that split method for a multi dimension array?

    Cause what i'm trying to achieve is:

    User enters their name (firstname, middlename, lastname);
    Then they get stored seperate into an array.
    a count, so each time a person enters a new name it doesnt get over written by the previous one.

    i.e.

    [0] [1] [2]
    [0] [firstname] [middle name] [last name]
    [1] [firstname] [middle name] [last name]
    [2] [firstname] [middle name] [last name]

    i'm not too sure how to implement this using the split method when it only accepts a single array.

    please help
    thanks
    You had to have think about an approach to do this from single dimension array to multi-dimension array. Simply you can take three elements at once from the single dimensional array and put it in single element in the other array.

  8. #8
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    If you must, simply define an index variable, nameCount. You should already have a String[][]; I'll call it names. Since this is an exercise, I'll show you the quick and wrong way to handle indexes.

    names[nameCount++] = inputString.split(" +");

    Read up on Java arrays. Multidimensional arrays are actually arrays of arrays, so you can assign an existing array to an array element.

  9. #9
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default

    I thought about if Strings are initialized by NULL by default i could enter "Firstname" "Middlename" "Lastname" and the next time somebody enters it again "Firstname" "--" "Lastname" and where "--" would equal NULL just add Lastname+1 so it would be in the lastname element.

    i.e.
    firstname middle lastname
    0 1 2
    0 first middle last
    1 first last <middle not entered
    2

    would be:

    firstname middle lastname
    0 1 2
    0 first middle last
    1 first NULL last <middlename+1
    2


    would that be possible? Or would i have to use an array list?

    Thanks

  10. #10
    poo
    poo is offline Member
    Join Date
    Jan 2009
    Posts
    1
    Rep Power
    0

    Default

    hi everyone.. how would u read in a file then stringtoken the text in the file and then assign each part of the text into different arrays....

    and the lines in the file are divide by @..

    i m new to java and i m so confused.. basically i m programming the game millionare.... so i duno how to pull the questions and choices frm a file into different arrays?? plzz help

  11. #11
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    poo: Please create a new thread with your question...

    I think you need to read up on arrays. Multidimensional arrays in Java don't work like arrays in some other languages I used, and they can be very confusing until you fully understand what Java did.

    Again, you will save yourself time by doing the following:

    1. Use String.split() to get the pieces.
    2. Create a simple class Name that will accept the pieces in a String[] constructor parameter and that defines properties first, middle, last.
    3. In the constructor, put as much logic as you like to figure out what the pieces mean. For example, one piece means a first name with no middle or last names, two pieces means a first and a last name, three pieces means a first, a middle, and a last name. Ignore more than three pieces. Note that this logic is overly simplistic for a real application.
    4. Create an ArrayList<Name> and put each name in the list.

    With Java, the approach that seems simplest at first will generally end up being very difficult. Learn to do it the "right way" from the start, and you will get good results quickly.

  12. #12
    phil128 is offline Member
    Join Date
    Sep 2008
    Posts
    40
    Rep Power
    0

    Default

    Ok Steve. Thanks for your help and to other people for spending time on this problem, i'll will get my head around!

    Again, many thanks

Similar Threads

  1. Array splitting
    By Lunarion in forum New To Java
    Replies: 3
    Last Post: 04-17-2009, 09:00 AM
  2. comparing elements in array
    By garyscott101 in forum New To Java
    Replies: 14
    Last Post: 12-10-2008, 04:01 PM
  3. Replies: 8
    Last Post: 08-27-2008, 11:45 PM
  4. splitting string and replacing
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-11-2007, 04:08 PM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 06:33 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
  •