Results 1 to 12 of 12
- 01-09-2009, 11:52 PM #1
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
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
- 01-09-2009, 11:55 PM #2
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
Essentially:
Read about the Java regular expressions API to find out more.Java Code:String[] elements = str.split(" +");Neil Coffey
Javamex - Java tutorials and performance info
- 01-10-2009, 02:32 AM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
And also read more about split() on the String class API.
- 01-10-2009, 09:29 PM #4
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
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
- 01-10-2009, 11:23 PM #5
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".
- 01-11-2009, 02:40 AM #6
Senior Member
- Join Date
- Nov 2008
- Posts
- 286
- Rep Power
- 5
I'm guessing this is just an exercise, not a production system. Strictly, people can also have spaces in their surname...
Neil Coffey
Javamex - Java tutorials and performance info
- 01-11-2009, 03:45 AM #7
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
- 01-11-2009, 03:58 AM #8
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.
- 01-11-2009, 08:17 PM #9
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
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
- 01-11-2009, 11:34 PM #10
Member
- Join Date
- Jan 2009
- Posts
- 1
- Rep Power
- 0
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
- 01-12-2009, 12:02 AM #11
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.
- 01-12-2009, 11:51 AM #12
Member
- Join Date
- Sep 2008
- Posts
- 40
- Rep Power
- 0
Similar Threads
-
Array splitting
By Lunarion in forum New To JavaReplies: 3Last Post: 04-17-2009, 08:00 AM -
comparing elements in array
By garyscott101 in forum New To JavaReplies: 14Last Post: 12-10-2008, 03:01 PM -
How to check whether two elements are available in an array?
By venkatteshb in forum New To JavaReplies: 8Last Post: 08-27-2008, 10:45 PM -
splitting string and replacing
By itsme in forum New To JavaReplies: 1Last Post: 12-11-2007, 03:08 PM -
Help with array of elements
By zoe in forum New To JavaReplies: 1Last Post: 07-24-2007, 05:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks