Results 1 to 7 of 7
  1. #1
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default newbish question

    Good evening,

    Suppose, there is a file that has 1 line
    Java Code:
    TAGS = 107,35,75,88
    Suppose there needs to be a task to read this line into a String[]
    See my newbish Java and questions below (code works)

    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++;
    }
    My question to you is:

    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 01:11 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by mac View Post
    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?
    Do you believe that split is more expensive than reading the line from the file? Have you tested this in a profiler? This sounds a bit like premature optimization.

  3. #3
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    You could use indexOf and substring to split it yourself.
    I die a little on the inside...
    Every time I get shot.

  4. #4
    mac's Avatar
    mac
    mac is offline Member
    Join Date
    Dec 2009
    Location
    St. Petersburg, Russia
    Posts
    81
    Rep Power
    0

    Default

    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 05:44 PM.

  5. #5
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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()]);
    Didn't test, but should work.

    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.

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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:
    Java Code:
    String[] tags = new String[myTags.split(",").length];
    
    int i = 0;
    for (String x : myTags.split(",")) {
    	tags[i] = x;
    	i++;
    }
    What is wrong with this?
    Java Code:
    String[] tags = myTags.split(",");
    -Gary-

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Forget the part about using substring() to get rid of "TAGS = " -- it looks like the Properties.get() method does that for you already.

    -Gary-

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. Question
    By Java_Fanatic in forum New To Java
    Replies: 10
    Last Post: 10-14-2009, 12:18 AM
  3. Hello everyone and my first question
    By htetnaing in forum New To Java
    Replies: 3
    Last Post: 01-26-2009, 04:49 PM
  4. question
    By ayoood in forum Java Software
    Replies: 6
    Last Post: 07-07-2008, 02:32 PM
  5. Need help with a question please
    By sonal in forum New To Java
    Replies: 1
    Last Post: 11-29-2007, 10:17 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
  •