Results 1 to 10 of 10
  1. #1
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default format content of String[]

    lets say i have an array of String[] with this on it

    Java Code:
    String[] file = new String[3];
    file [0] = "abc.txt";
    file [1]= "def.txt";
    file [2] = "ghi.txt";
    HOw can i format its contents so that i get rid of the .txt on each component. Do i need to save them to a file, then read the file (get the line up the the dot) and then store it back the array or is there a simpler way!
    Last edited by eponcedeleon; 02-22-2010 at 06:35 PM. Reason: tagging code..

  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 eponcedeleon View Post
    lets say i have an array of String[] with this on it

    file [0] = abc.txt
    file [1]= def.txt
    file [2] = ghi.txt

    HOw can i format its contents so that i get rid of the .txt on each component. Do i need to save them to a file, then read the file (get the line up the the dot) and then store it back the array or is there a simpler way!
    ???
    do you mean that file is a String array? Or is it a File object? Your post is somewhat confusing.

    Also is abc.txt a String? If so, should you enclose it in quotes?

    It appears that you are posting uncompilable code, and this will serve no purpose other than to confuse. You may wish to re-state your question.

  3. #3
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default

    The array is of type String...

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eponcedeleon View Post
    lets say i have an array of String[] with this on it

    file [0] = abc.txt
    file [1]= def.txt
    file [2] = ghi.txt

    HOw can i format its contents so that i get rid of the .txt on each component. Do i need to save them to a file, then read the file (get the line up the the dot) and then store it back the array or is there a simpler way!
    Read the API documentation for the String class and pay special attention to the indexOf( ... ) method(s) and the substring( ... ) method(s).

    kind regards,

    Jos

  5. #5
    eponcedeleon is offline Member
    Join Date
    Feb 2010
    Posts
    16
    Rep Power
    0

    Default

    any can just post an example of just 1, so i can understand it!

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by eponcedeleon View Post
    any can just post an example of just 1, so i can understand it!
    No, it's so simple you can do it yourself: use the indexOf( ... ) method to find the start of the ".txt" String. Use the substring( ... ) method to chop that part of the String off. It's much more 'fun' to do it yourself instead of bluntly copying and pasting what I have written. I know how to do it, I knew that years ago already, no matter the programming language; it's your turn now.

    kind regards,

    Jos

  7. #7
    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 eponcedeleon View Post
    any can just post an example of just 1, so i can understand it!
    Agree with Jos. You're always far better off to go to the API and try on your own first. Then if it still doesn't work after several attempts, come on back with your code. Otherwise you will appear to be asking for spoon feeding which will teach you nothing in the long run.

  8. #8
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Loop through the array and modify each String individually. I suggest checking out the 'split' method in the String API.

  9. #9
    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 toadaly View Post
    Loop through the array and modify each String individually. I suggest checking out the 'split' method in the String API.
    I was thinking more about the replaceAll(...) method myself.

  10. #10
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

Similar Threads

  1. Convert String to Time format
    By plasma in forum New To Java
    Replies: 1
    Last Post: 12-16-2009, 08:58 AM
  2. String.format not in javadoc
    By ortollj in forum New To Java
    Replies: 3
    Last Post: 11-08-2009, 05:14 PM
  3. spliting a string and checking each token's format
    By Implode in forum New To Java
    Replies: 1
    Last Post: 10-18-2009, 08:41 PM
  4. Replies: 0
    Last Post: 04-02-2009, 07:02 PM
  5. Replies: 1
    Last Post: 04-09-2008, 02:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •