This week's giveaway is in the EJB and other Java EE Technologies forum. We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line! See this thread for details.
Hi everybody, I need to extract individual portions out of a string that I read in from a text file. I have a string like the following: May, April F 255-2190 1 5 2 3 3 2 3 3 2 3 5 5 and I need to extract the name, last name, gender, phone, etc. I can successfully read from the file with following method:
I have no idea how to approach this, any help is greatly appreciated [ April 08, 2004: Message edited by: Ben Buchli ] [ April 08, 2004: Message edited by: Ben Buchli ]
It looks like your strategy for reading in each line of text from the file is fine. So, let's ignore that part, and work on the question of how to break your String apart. Is each line of the text file guaranteed to be of the exact same format with no variations? Or are you supposed to consider and handle different and/or broken line formats? As far as breaking up and getting certain parts of a String are concerned, take a look at the String class documentation for a few methods that you might find useful. (Hint: You might like to use the split method, one or more of the indexOf methods, and one or more of the substring methods.) For breaking the string apart, you might find a StringTokenizer to be useful. Note that you won't likely need this if you're going to use the String.split method. So, how's it going?
Dirk, thanks for your reply. The strings are basically always the same, except that there can be a middle name. May, April F 255-2190 1 5 2 3 3 2 3 3 2 3 5 5 or May, April June F 255-2190 1 5 2 3 3 2 3 3 2 3 5 5 So my strategy was to read in the file line by line and store each line in a listArray. Then, when the file is read completely, I would want to get one String at a time and split the string so that i can store the information in a Person array (Person is a class that I wrote to store name phone, gender, etc). So my problem is, how do i know if there is a middle name or not? I was thinking I would check with the length() method if the string is longer than one, which means I havent reached the gender yet. However, what if the user gives only middle initial? Do I just have to require them to write out the whole middle name? I have played around with it a little bit, but the code I wrote seems to be too repetitious and complicated. I am quite sure there is an easier way to do the task. Anyway, I put the code here, so that I can embarrass myself...
So this code works fine, except for the case when there is a middle name. however, as i mentioned, I think there should be an easier way to accomplish my task. Thanks for any help!
[ edited to break apart long unbroken comment line in code block (and to remove the evil tab characters -ds ] [ April 10, 2004: Message edited by: Dirk Schreckmann ]
Ben, Since the strings are bacially the same and seperated by space, you can use split() in String class from j2se1.4 that returns a String array to simplify the process. String items = s.split(" "); // use space as delim then pay some attention to check if middle name is there. HTH
Joined: Mar 26, 2004
Hi guys, thanks for your help. But I need some more... If I read the first line into an array with the .split( " " ) method, it'll be stored in the following way. Position 0 May, Position 1 April Position 2 Position 3 Position 4 Position 5 Position 6 Position 7 Position 8 Position 9 Position 10 Position 11 Position 12 F Position 13 255-2190 Position 14 1 Position 15 5 Position 16 2 Position 17 3 Position 18 3 Position 19 2 Position 20 3 Position 21 3 Position 22 2 Position 23 3 Position 24 5 Position 25 5 the line stored in the text file looks like:
How do I make sure that the empty strings (in pos. 2-11) are not stored? I cannot just dump these, because with the next line it wont be pos. 2-11 because of a middle name, or longer first/last name. How come the split() method treats the spaces between first name and gender as a string to be stored? is it because I define the regex to be exactly one space with split( " " )?? How do I then define one or more spaces? Do I have to compile a regex that looks something like "* "?? Or do I have to use the stringTokenizer?? Any help is greatly appreciated! Ben [ April 10, 2004: Message edited by: Ben Buchli ] [ April 10, 2004: Message edited by: Ben Buchli ]