Results 1 to 7 of 7
Thread: newbish question
- 03-27-2010, 12:08 AM #1
newbish question
Good evening,
Suppose, there is a file that has 1 line
Suppose there needs to be a task to read this line into a String[]Java Code:TAGS = 107,35,75,88
See my newbish Java and questions below (code works)
My question to you is:Java Code:private Properties inputFileProperties = new Properties(); String myTags = (String) inputFileProperties.get("TAGS"); String[] tags = new String[myTags.split(",").length]; int i = 0; for (String x : myTags.split(",")) { tags[i] = x; i++; }
1. Any way i can avoid "split"? I am mindful of that fact that split is expensive
2. Is there a better way to get a comma delimited string and push it into an array?
Assume:
1. I have no control over how many elements are comma delimited in the line
2. I don't have to do any data validation
Thank you very much for reading this and responding
PS: If you tell me how i can load it up from properties straight into a String array, it would be above awesome!Last edited by mac; 03-27-2010 at 12:11 AM.
-
- 03-27-2010, 10:48 AM #3
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
You could use indexOf and substring to split it yourself.
I die a little on the inside...
Every time I get shot.
- 03-27-2010, 03:38 PM #4
Thank you. I figured substring is better, but, could you provide an example of how i can take a string and store comma delimited vales into a String []
For that matter, if i have something like this
TAGS = 1,2,3,4,5,6
How can i tell how many elements this string has using substring, using "," as a delimiter?
PS:
Also, assume length of text between commas will vary.Last edited by mac; 03-27-2010 at 04:44 PM.
- 03-27-2010, 07:29 PM #5
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
Didn't test, but should work.Java Code:String input = "af,agy,3,d,hi,6"; ArrayList<String> stringAL = new ArrayList<String>(); while (true) { int index = input.indexOf(','); if (index == -1) // char not found index = input.length(); String first = input.substring(0,index); stringAL.add(first); if (index != input.length()) input = input.substring(index+1); else break; } String[] stringArray = stringAL.toArray(new String[stringAL.size()]);
Instead of cutting the input string into pieces, you could also use indexOf(int fromIndex, int char) instead of indexOf(int char).I die a little on the inside...
Every time I get shot.
- 03-27-2010, 08:11 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I would definitely use substring() to get rid of the "TAGS = " part, but after that, why would you not want to use split()? It seems to me that split() does precisely what you want here, but you are making it much more complicated than it needs to be with this:
What is wrong with this?Java Code:String[] tags = new String[myTags.split(",").length]; int i = 0; for (String x : myTags.split(",")) { tags[i] = x; i++; }
-Gary-Java Code:String[] tags = myTags.split(",");
- 03-27-2010, 08:17 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Similar Threads
-
Question mark colon operator question
By orchid in forum Advanced JavaReplies: 9Last Post: 12-19-2010, 08:49 AM -
Question
By Java_Fanatic in forum New To JavaReplies: 10Last Post: 10-13-2009, 11:18 PM -
Hello everyone and my first question
By htetnaing in forum New To JavaReplies: 3Last Post: 01-26-2009, 03:49 PM -
question
By ayoood in forum Java SoftwareReplies: 6Last Post: 07-07-2008, 01:32 PM -
Need help with a question please
By sonal in forum New To JavaReplies: 1Last Post: 11-29-2007, 09:17 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks